Python Pandas Dataframe条件If,Elif,Else [英] Python Pandas Dataframe Conditional If, Elif, Else
问题描述
在Python Pandas DataFrame
中,如果搜索字词"列包含连接的竖线分隔列表中的任何可能的字符串,则尝试将特定标签应用于行.如果用熊猫其他语句,我该怎么做?
In a Python Pandas DataFrame
, I'm trying to apply a specific label to a row if a 'Search terms' column contains any possible strings from a joined, pipe-delimited list. How can I do conditional if, elif, else statements with Pandas?
例如:
df = pd.DataFrame({'Search term': pd.Series(['awesomebrand inc', 'guy boots', 'ectoplasm'])})
brand_terms = ['awesomebrand', 'awesome brand']
footwear_terms = ['shoes', 'boots', 'sandals']
#Note: this does not work
if df['Search term'].str.contains('|'.join(brand_terms)):
df['Label'] = 'Brand'
elif df['Search term'].str.contains('|'.join(footwear_terms)):
df['Label'] = 'Footwear'
else:
df['Label'] = '--'
所需输出示例:
Search Term Label
awesomebrand inc Brand
guy boots Footwear
ectoplasm --
我尝试将.any()
附加到contains()
语句的末尾,但是它将Brand
标签应用到每一行.
I've tried appending .any()
to the ends of the contains()
statements but it applies the Brand
label to every row.
我遇到的大多数示例都是在比较列值==
是否等于(不是我想要的值)还是执行数字比较,而不是文本字符串比较.
Most of the examples I come across are comparing if a column value ==
is equal to (not what I want) or are performing numeric comparisons, not text string comparisons.
推荐答案
这是使用str.contains()
和np.where()
In [26]:
np.where(df['Search term'].str.contains('|'.join(brand_terms)),
'Brand',
np.where(df['Search term'].str.contains('|'.join(footwear_terms)),
'Footwear',
'--'))
Out[26]:
array(['Brand', 'Footwear', '--'],
dtype='|S8')
您可以像这样分配给df['Label']
In [27]: df['Label'] = np.where(df['Search term'].str.contains('|'.join(brand_terms)),
....: 'Brand',
....: np.where(df['Search term'].str.contains('|'.join(footwear_terms)),
....: 'Footwear',
....: '--'))
In [28]: df
Out[28]:
Search term Label
0 awesomebrand inc Brand
1 guy boots Footwear
2 ectoplasm --
这篇关于Python Pandas Dataframe条件If,Elif,Else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!