简体   繁体   中英

How to map value from nested dictionary to multiple columns in dataframe or from 3 column dataframe to main dataframe?

For example: I have df like this:

id      Status         Country       Income
1          4               2          3
2          5               3          2 

and dictionary like this:

d_dict = {Status : { '4':'Married', '5':'UnMarried'},
        Country: { '2': 'Japan' , '3': 'China'},
        Income: {'3': "5000-10000", 2: "11000-20000"}}

I want to map the values based on nested dictionary. I can do for one column like this:

for k,v in d_dict.items():
    max_d[k] = max(v, key=v.get)
df['Status'] = df['Status'].map(max_d)

But I have more than 2000 columns and I am not sure how I can do for multiple columns.

I tried also with replace but not working.

df=df.astype(str).replace(d_dict)

For me secons solution working nice - only necessary numbers in nested keys are strings:

d_dict = {'Status' : { '4':'Married', '5':'UnMarried'},
        'Country': { '2': 'Japan' , '3': 'China'},
        'Income': {'3': "5000-10000", '2': "11000-20000"}}


df = df.astype(str).replace(d_dict)
print (df)
  id     Status Country       Income
0  1    Married   Japan   5000-10000
1  2  UnMarried   China  11000-20000

So you can try convert nested keys to strings:

d_dict = {'Status' : { '4':'Married', '5':'UnMarried'},
        'Country': { '2': 'Japan' , '3': 'China'},
        'Income': {3: "5000-10000", 2: "11000-20000"}}

d_dict = {k: {str(k1): v1 for k1, v1 in v.items()} for k,v in d_dict.items()}

df = df.astype(str).replace(d_dict)
print (df)
  id     Status Country       Income
0  1    Married   Japan   5000-10000
1  2  UnMarried   China  11000-20000

Or convert all keys to integers:

d_dict = {k: {int(k1): v1 for k1, v1 in v.items()} for k,v in d_dict.items()}

df = df.replace(d_dict)
print (df)
   id     Status Country       Income
0   1    Married   Japan   5000-10000
1   2  UnMarried   China  11000-20000

If I'm understanding correctly you can use:

    for k in d_dict.keys():
        df[k] = df[k].apply(lambda x: d_dict[k][str(x)])

But be aware that your dict keys must be strings (therefore str(x) and not x ) otherwise raises error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM