英文:
Map DataFrame column to 2 different dictionaries
问题
df['allowance'] = df.apply(lambda row: d1[row['country']][row['age']], axis=1)
英文:
Provided is code for how much allowance a kid would get based on country and age.
I can get the right allowance for a kid using a for loop but would like to
just use the map function instead.
If the country feature wasn't there I would just have been able to map
a kid's age value directly to 1 dict. The problem now is that I have 2 dicts I want
to map df['age']
to. Is it possible to get the right allowance using
only the map function and avoiding the for loop?
import pandas as pd
d1 = {
'canada': {
'0-5': 5,
'6-8': 8,
'9-10': 9,
'11-15': 14,
'16-18': 24},
'us': {
'0-5': 7,
'6-8': 9,
'9-10': 10,
'11-15': 15,
'16-18': 35}}
df = pd.DataFrame.from_dict({
'name': ['Askeladd', 'Torkel'],
'country': ['canada', 'us'],
'age': ['0-5', '16-18']})
df2 = df.copy()
df2['allowance'] = [0]*2
# Gives right answer
for i in range(len(df2)):
df2['allowance'].iloc[i] = d1[df2.iloc[i,:].loc['country']][df2.iloc[i,:].loc['age']]
# Gives wrong answer
df['allowance'] = df['age'].map(d1['us'])
答案1
得分: 1
连接
首先将 d1
转化为一个 Series:
s1 = (pd.DataFrame(d1).rename_axis(index='age', columns='country')
.unstack().rename('allowance'))
-
或者使用更手动的方法:
d1_flat = {(co, age): x for co,a in d1.items() for age,x in a.items()} s1 = pd.Series(d1_flat, name='allowance').rename_axis(['country', 'age'])
然后进行连接:
df.join(s1, on=['country', 'age'])
name country age allowance
0 Askeladd canada 0-5 5
1 Torkel us 16-18 35
如果你需要进一步的帮助,请告诉我。
英文:
Join
First make a Series out of d1
:
s1 = (pd.DataFrame(d1).rename_axis(index='age', columns='country')
.unstack().rename('allowance'))
-
Or a more manual approach:
d1_flat = {(co, age): x for co,a in d1.items() for age,x in a.items()} s1 = pd.Series(d1_flat, name='allowance').rename_axis(['country', 'age'])
Then join:
df.join(s1, on=['country', 'age'])
name country age allowance
0 Askeladd canada 0-5 5
1 Torkel us 16-18 35
答案2
得分: 1
你可以从你的字典创建一个数据框,然后与原始数据框合并:
df.merge(pd.DataFrame(d1).stack().rename('allowance'),
left_on=['age','country'], right_index=True, how='left')
)
输出:
name country age allowance
0 Askeladd canada 0-5 5
1 Torkel us 16-18 35
英文:
You can create a dataframe from your dictionary then merge with original:
df.merge(pd.DataFrame(d1).stack().rename('allowance'),
left_on=['age','country'], right_index=True, how='left')
)
Output:
name country age allowance
0 Askeladd canada 0-5 5
1 Torkel us 16-18 35
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论