简体   繁体   English

如果从熊猫数据框列值中删除句子,则删除第一个单词

[英]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.

相关问题 Pandas - 通过比较同一数据框中的其他列值(单词)来提取列中不匹配的句子单词 - Pandas - Extract unmatched word of sentence in a column by comparing other columns value (word) in the same data frame 使用Python从数据框中删除仅具有单个单词的列值 - Remove Column values having Single word only from a Data Frame using Python 从 pandas 数据帧的标题中删除常用词 - Remove common word from headers in pandas data frame 如何从 pandas 数据框列中删除不符合条件的值? - How to remove values that do not meet condition from pandas data frame column? 从pandas数据框列的词典列表中获取第一个值 - Get first value from a list of dictionaries in pandas data frame column Pandas 数据框列的 BERT Word Embedding - BERT Word Embedding for column of pandas data frame 在整个数据框中查找第一列元素并返回每行前面的第一列值(熊猫) - find first column elements in entire data frame and return first column values in front of each row(Pandas) 从句子中删除第一个单词并返回剩余的字符串 - Remove first word from the sentence and return remaining string 从数据框中获取文本的最佳方法,先按句子然后按词标记 - Best way to take text from data frame, tokenize by sentence then by word 从Pandas数据框列生成列表(包括缺失值) - Generate a list (including missing values) from a Pandas data frame column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM