将DataFrame列映射到两个不同的字典。

huangapple go评论80阅读模式
英文:

Map DataFrame column to 2 different dictionaries

问题

  1. 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?

  1. import pandas as pd
  2. d1 = {
  3. 'canada': {
  4. '0-5': 5,
  5. '6-8': 8,
  6. '9-10': 9,
  7. '11-15': 14,
  8. '16-18': 24},
  9. 'us': {
  10. '0-5': 7,
  11. '6-8': 9,
  12. '9-10': 10,
  13. '11-15': 15,
  14. '16-18': 35}}
  15. df = pd.DataFrame.from_dict({
  16. 'name': ['Askeladd', 'Torkel'],
  17. 'country': ['canada', 'us'],
  18. 'age': ['0-5', '16-18']})
  19. df2 = df.copy()
  20. df2['allowance'] = [0]*2
  21. # Gives right answer
  22. for i in range(len(df2)):
  23. df2['allowance'].iloc[i] = d1[df2.iloc[i,:].loc['country']][df2.iloc[i,:].loc['age']]
  24. # Gives wrong answer
  25. df['allowance'] = df['age'].map(d1['us'])

答案1

得分: 1

连接

首先将 d1 转化为一个 Series:

  1. s1 = (pd.DataFrame(d1).rename_axis(index='age', columns='country')
  2. .unstack().rename('allowance'))
  • 或者使用更手动的方法:

    1. d1_flat = {(co, age): x for co,a in d1.items() for age,x in a.items()}
    2. s1 = pd.Series(d1_flat, name='allowance').rename_axis(['country', 'age'])

然后进行连接:

  1. df.join(s1, on=['country', 'age'])
  1. name country age allowance
  2. 0 Askeladd canada 0-5 5
  3. 1 Torkel us 16-18 35

如果你需要进一步的帮助,请告诉我。

英文:

Join

First make a Series out of d1:

  1. s1 = (pd.DataFrame(d1).rename_axis(index='age', columns='country')
  2. .unstack().rename('allowance'))
  • Or a more manual approach:

    1. d1_flat = {(co, age): x for co,a in d1.items() for age,x in a.items()}
    2. s1 = pd.Series(d1_flat, name='allowance').rename_axis(['country', 'age'])

Then join:

  1. df.join(s1, on=['country', 'age'])
  1. name country age allowance
  2. 0 Askeladd canada 0-5 5
  3. 1 Torkel us 16-18 35

答案2

得分: 1

你可以从你的字典创建一个数据框,然后与原始数据框合并:

  1. df.merge(pd.DataFrame(d1).stack().rename('allowance'),
  2. left_on=['age','country'], right_index=True, how='left')
  3. )
  4. 输出
  5. name country age allowance
  6. 0 Askeladd canada 0-5 5
  7. 1 Torkel us 16-18 35
英文:

You can create a dataframe from your dictionary then merge with original:

  1. df.merge(pd.DataFrame(d1).stack().rename('allowance'),
  2. left_on=['age','country'], right_index=True, how='left')
  3. )

Output:

  1. name country age allowance
  2. 0 Askeladd canada 0-5 5
  3. 1 Torkel us 16-18 35

huangapple
  • 本文由 发表于 2023年5月7日 08:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定