4 回答

TA貢獻1827條經驗 獲得超4個贊
這個解決方案怎么樣,當你有一個新類別時,你所要做的就是將新類別添加到 cats 數組中。
import pandas as pd
import numpy as np
df = pd.DataFrame({'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5']})
cats = ["Pegasus","Metcon"]
df["Category"] = df["Product Name"].apply(lambda x: np.intersect1d(x.split(" "),cats)[0])
output
Product Name Category
0 Nike Zoom Pegasus Pegasus
1 All New Nike Zoom Pegasus 4 Pegasus
2 Metcon 3 Metcon
3 Nike Metcon 5 Metcon

TA貢獻1859條經驗 獲得超6個贊
>>> df = pd.DataFrame({'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5']})
>>> cats = ["Pegasus","Metcon"]
>>> df['Category'] = df["Product Name"].str.extract("(%s)" % "|".join(cats))
Product Name Category
0 Nike Zoom Pegasus Pegasus
1 All New Nike Zoom Pegasus 4 Pegasus
2 Metcon 3 Metcon
3 Nike Metcon 5 Metcon

TA貢獻1797條經驗 獲得超6個贊
怎么樣:
import pandas as pd
df = {'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5']}
c = set(['Metcon', 'Pegasus'])
categories = [c.intersection(pn.split(' ')) for pn in df['Product Name']]
df['Categories'] = categories
print(df)
>> {'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5'], 'Categories': [{'Pegasus'}, {'Pegasus'}, {'Metcon'}, {'Metcon'}]}

TA貢獻1802條經驗 獲得超10個贊
您的代碼存在的問題如下:
您傳遞的是產品,但在檢查時使用的是
df["Product Name"]
,這會返回整個系列。此外,返回值是產品。但根據預期的答案,要么是
Pegasus
要么Metcon
我想你想要這樣的東西。
def get_category(product):
if "Pegasus" in product:
return "Pegasus"
elif "Metcon" in product:
return "Metcon"
添加回答
舉報