[英]Remove first word if a sentence from pandas data frame column values
I have a data frame like this: 我有一个像这样的数据框:
df:
col1 col2
A blue berry
B nice water bottle
I want to remove first word from the col2 values, the final data frame will look like this: 我想从col2值中删除第一个单词,最后一个数据帧将如下所示:
df1:
col1 col2
A berry
B water bottle
How to do this in most effective way using pandas 如何使用熊猫以最有效的方式做到这一点
d['col2'] = d['col2'].apply(lambda x: ' '.join(x.split(' ')[1:]))
Output 产量
col1 col2
0 A berry
1 B water bottle
in your case 在你的情况下
df1.col2 = df1.col2.str.split(" ",1).str[1]
might do the trick. 可能会解决问题。 The .str
gives you vectorised string functionality on columns and and split takes an additional argument which says how often it will maximaly split. .str
为您提供了列上的矢量化字符串功能,而split带有一个附加参数,该参数表示将最大程度地拆分的频率。
Use split
by first whitespace with n=1
and then select second lists by indexing: 使用n=1
按第一个空格split
,然后通过索引选择第二个列表:
df['col2'] = df['col2'].str.split(n=1).str[1]
print (df)
col1 col2
0 A berry
1 B water bottle
Detail : 详细说明 :
print (df['col2'].str.split(n=1))
0 [blue, berry]
1 [nice, water bottle]
Name: col2, dtype: object
If performance is important and no missing values convert solution to list comprehension: 如果性能很重要并且没有缺失值,则将解决方案转换为列表理解:
df['col2'] = [x.split(maxsplit=1)[1] for x in df['col2']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.