3 回答
TA贡献1828条经验 获得超13个赞
使用字符串方法进行条件和提取所需的值,
pat = r'({})'.format('|'.join(d.values()))
cond = df['Fruit'].str.contains('|'.join(d.values()))
df.loc[cond, 'Fruit'] = df['Fruit'].str.extract((pat), expand = False)
Fruit Price
0 Sweet Mango 1
1 Apple 2
2 Banana 0
3 Banana 5
编辑:正如@user3483203 所建议的,您可以在提取模式后使用原始值填充缺失值。
df['Fruit'] = df['Fruit'].str.extract(pat).fillna(df.Fruit)
TA贡献1936条经验 获得超6个赞
IIUC,您可以使用apply()自定义函数:
import pandas as pd
df = pd.DataFrame([['Sweet Mango', 1],['Green Apple', 2],['Few blue Banana', 0],['Black Banana', 5]],
columns=['Fruit','Price'])
fruits = {'BN':'Banana', 'LM': 'Lemon', 'AP':'Apple', 'MG': 'Mango'}
def find_category(x):
return [k for k in fruits.values() if k in x][0]
df['Fruit'] = df['Fruit'].apply(find_category)
产量:
Fruit Price
0 Mango 1
1 Apple 2
2 Banana 0
3 Banana 5
添加回答
举报