在数据框列中找到字典的值并对其进行修改 [英] Find a value of a dictionary in dataframe column and modify it
问题描述
我现在正在处理DataFrames和Dictionary,但是我遇到了问题, 我有一本字典"水果"
I dealing with DataFrames and Dictionaries now, and i have a problem, I have a Dictionary "Fruits"
{BN:'Banana', LM:'Lemon', AP:'Apple' ..... etc}
还有一个DataFrame-" Stock ":
And a DataFrame- "Stock":
Fruit Price
0 Sweet Mango 1
1 Green Apple 2
2 Few blue Banana 0
3 Black Banana 5
我想做下一件事情:
用Fruits.values()
替换Stock['Fruit']
中的所有值,方法是:
如果水果"中的值出现在Stock['Fruit']
行中,则它将通过以下方式替换:
I wand to do the next thing:
replace all the values from Stock['Fruit']
with the Fruits.values()
this way:
if the value from Fruits appears in the Stock['Fruit']
row it will be replaced this way:
很少有蓝色香蕉 ---> 香蕉
Few blue Banana ---> Banana
黑色香蕉 ---> 香蕉
现在,DataFrame Stock 会看起来像这样:
now the DataFrame Stock will look this way:
Fruit Price
0 Sweet Mango 1
1 Green Apple 2
2 Banana 0
3 Banana 5
我找到了不同的代码来替换或检查Dicitionary中的值是否出现在DataFrame中
I found different codes to replace or to check if values from the Dicitionary appears in the DataFrame
Stock['Fruit'] = Stock.Fruit.map(Fruits)
if (Fruits.values() in Stock['Fruit'] for item in Stock)
any('Mango' in Stock['Fruit'] for index,item in Stock.iterrows())
但是我找不到任何可以更新DataFrame行的东西
But i cant find any thing to update the rows of the DataFrame
推荐答案
使用字符串方法确定条件并提取所需的值,
Use string methods for condition and extracting required values,
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的建议,一旦提取出图案,就可以用原始的填充缺失的值.
As @user3483203 suggested, you can fill the missing values with original once the pattern is extracted.
df['Fruit'] = df['Fruit'].str.extract(pat).fillna(df.Fruit)
这篇关于在数据框列中找到字典的值并对其进行修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!