我正在使用熊猫。这是我的 df:
df = {'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5']}
我想搜索每个字符串值并仅提取产品类别,然后将提取的字符串值放在另一列(“类别”)中。您可能会注意到,产品名称没有正式的命名约定,因此 .split() 不适合使用。
最终结果应如下所示:
df = {'Product Name': ['Nike Zoom Pegasus', 'All New Nike Zoom Pegasus 4', 'Metcon 3', 'Nike Metcon 5'], 'Category': ['Pegasus', 'Pegasus', 'Metcon', 'Metcon]}
我当前的代码是这样的,但出现错误:
def get_category(product):
if df['Product Name'].str.contains('Pegasus') or df['Product Name'].str.contains('Metcon'):
return product
df['Category'] = df['Product Name'].apply(lambda x: get_category(x))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
希望你能帮忙。谢谢!
GCT1015
BIG阳
FFIVE
守候你守候我
相关分类