如何在 Python 中拆分字段并在另一列中返回值?



我有一个包含如下数据的csv:

"A17W-1000010931-RED-14"

我正在尝试按第三个连字符拆分数据,以便它返回之前的所有内容,如下所示:

"A17W-1000010931-RED"

这很可能是一件容易的事,但我正在努力寻找答案。

提前非常感谢。

您可以使用.replace

df["your column"] = df["your column"].replace(
r"(.*)-(.*)-(.*)-.*", "\1-\2-\3", regex=True
)

Python的几个通用字符串操作方法。可能熊猫有更好的东西。

>>> import re
>>> re.findall("^[^-]*-[^-]*-[^-]*", "A17W-1000010931-RED-14")[0]
'A17W-1000010931-RED'

另一种方式:

>>> "-".join("A17W-1000010931-RED-14".split("-")[:3])
'A17W-1000010931-RED'

你可以试试这个

string= "A17W-1000010931-RED-14"
es=string.split("-")[:3]
jes='-'.join(es)
merge='"'+jes+'"'

如果您的打印合并,它将显示所需结果的输出

>假设您的列是名为seriespandas.Series对象

series = pd.Series(["A17W-1000010931-RED-14", 'ABC-123-COLOR-Other-Stuff'] * 2)
series
0       A17W-1000010931-RED-14
1    ABC-123-COLOR-Other-Stuff
2       A17W-1000010931-RED-14
3    ABC-123-COLOR-Other-Stuff
dtype: object

str.find、字符串切片和list理解

[x[:x.find('-', x.find('-', x.find('-') + 1) + 1)] for x in series]
['A17W-1000010931-RED',
'ABC-123-COLOR',
'A17W-1000010931-RED',
'ABC-123-COLOR']

我正在尝试尽量减少新对象的创建。

  • 查找'-'的第一个位置并向其添加一个位置

    x.find('-') + 1
    
  • 我们可以将起始位置传递给str.find,以便它只搜索超过某个点......就像第一个'-'在哪里一样。 但以上正是我想开始寻找第二个'-'的起点

    x.find('-', x.find('-') + 1) + 1
    
  • 重复该过程以找到第三个

    x.find('-', x.find('-', x.find('-') + 1) + 1)
    
  • 现在我有了位置,我们可以轻松地切开字符串

    x[:x.find('-', x.find('-', x.find('-') + 1) + 1)]
    

另一种方法是str.splitstr.join

# Note that I limit this to 3 splits with this
#                      ↓
['-'.join(x.split('-', 3)[:3]) for x in series]

这很好。

最新更新