2 回答
TA贡献1845条经验 获得超8个赞
无需循环。您可以使用.isin()withnp.select()根据条件返回结果。见下面的代码:
import pandas as pd
import numpy as np
list_drinks=['Water','Juice','Tea']
list_food=['Apple','Orange']
data = {'Price': ['1', '5','3'],
'Product': ['Juice','book','Pen']}
df = pd.DataFrame(data)
df['Category'] = np.select([(df['Product'].isin(list_drinks)),
(df['Product'].isin(list_food))],
['drinks',
'food'], 'Other')
df
Out[1]:
Price Product Category
0 1 Juice drinks
1 5 book Other
2 3 Pen Other
下面,我将代码分解为更详细的内容,以便您可以了解它是如何工作的。我也根据你的评论略有改变。我使用列表理解和 来检查列表中的值是否位于数据帧中的值的子字符串中in。为了提高匹配率,我还将 as 全部小写与 进行比较.lower():
import pandas as pd
import numpy as np
list_drinks=['Water','Juice','Tea']
list_food=['Apple','Orange']
data = {'Price': ['1', '5','3'],
'Product': ['green Juice','book','oRange you gonna say banana']}
df = pd.DataFrame(data)
c1 = (df['Product'].apply(lambda x: len([y for y in list_drinks if y.lower() in x.lower()]) > 0))
c2 = (df['Product'].apply(lambda x: len([y for y in list_food if y.lower() in x.lower()]) > 0))
r1 = 'drinks'
r2 = 'food'
conditions = [c1,c2]
results= [r1,r2]
df['Category'] = np.select(conditions, results, 'Other')
df
Out[1]:
Price Product Category
0 1 green Juice drinks
1 5 book Other
2 3 oRange you gonna say banana food
TA贡献1825条经验 获得超4个赞
这是一个替代方案 -
import itertools
import pandas as pd
list_drinks={'Water','Juice','Tea'}
list_food={'Apple','Orange'}
data = pd.DataFrame({'Price': ['1', '5','3'], 'Product': ['Juice','book', 'Pen']})
category = list()
for prod in data['Product']:
if prod in list_food:
category.append("Food")
elif prod in list_drinks:
category.append("drinks")
else:
category.append("Other")
data['Category']= category
print(data)
输出-
Price Product Category
1 Juice drinks
5 book Other
3 Pen Other
添加回答
举报