![](/img/trans.png)
[英]How to check if a column has a word based on words from another column with three different conditions in Pandas?
[英]Python/pandas - Add word in column based on words in another column
我正在使用带有pandas的xlsx文件,如果前一列在预定义的bodyparts列表中包含一个单词,我想在列中添加单词“bodypart”。
原始数据帧:
Sentence Type
my hand NaN
the fish NaN
结果数据框:
Sentence Type
my hand bodypart
the fish NaN
我没有尝试过任何工作。 我觉得我错过了一些非常明显的东西。 这是我最后一次(失败)的尝试:
import pandas as pd
import numpy as np
bodyparts = ['lip ', 'lips ', 'foot ', 'feet ', 'heel ', 'heels ', 'hand ', 'hands ']
df = pd.read_excel(file)
for word in bodyparts :
if word in df["Sentence"] : df["Type"] = df["Type"].replace(np.nan, "bodypart", regex = True)
我也试过这个,使用变体“NaN”和NaN作为str.replace的第一个参数:
if word in df['Sentence'] : df["Type"] = df["Type"].str.replace("", "bodypart")
任何帮助将不胜感激!
一个肮脏的解决方案将涉及检查两组的交集。
集合A是您的身体部位列表,集合B是句子中的单词集合
df['Sentence']\
.apply(lambda x: 'bodypart' if set(x.split()) \
.symmetric_difference(bodyparts) else None)
最简单的方法:
df.loc[df.Sentence.isin(bodyparts),'Type']='Bodypart'
在你必须丢弃bodyparts
空间之前:
bodyparts = {'lip','lips','foot','feet','heel','heels','hand','hands'}
df.Sentence.isin(bodyparts)
选择好行,并Type
要设置的列。 .loc
是允许修改的索引器。
您可以创建一个正则表达式来搜索单词边界,然后将其用作str.contains
的参数,例如:
import pandas as pd
import numpy as np
import re
bodyparts = ['lips?', 'foot', 'feet', 'heels?', 'hands?', 'legs?']
rx = re.compile('|'.join(r'\b{}\b'.format(el) for el in bodyparts))
df = pd.DataFrame({
'Sentence': ['my hand', 'the fish', 'the rabbit leg', 'hand over', 'something', 'cabbage', 'slippage'],
'Type': [np.nan] * 7
})
df.loc[df.Sentence.str.contains(rx), 'Type'] = 'bodypart'
给你:
Sentence Type
0 my hand bodypart
1 the fish NaN
2 the rabbit leg bodypart
3 hand over bodypart
4 something NaN
5 cabbage NaN
6 slippage NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.