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

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

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

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:

确定