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
添加回答
舉報