Title: How to map value from nested dictionary to multiple columns in dataframe or from 3 column dataframe to main dataframe?
Tags: python;pandas;dataframe;dictionary
Question: 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)

Here is the accepted answer: 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

Here is another answer: 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.
Comment for this answer: I am getting key error for the columns which are not in dictionary. I have not all the columns are in dictionary and which are also not required.
Comment for this answer: sorry, no brackets,
if k in df.columns:

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:03/11/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><pandas><dataframe><dictionary>', 'Title': 'How to map value from nested dictionary to multiple columns in dataframe or from 3 column dataframe to main dataframe?', 'ViewCount': 501, 'id': 60631736, 'user_AboutMe': '', 'user_AccountId': 6509456.0, 'user_DisplayName': 's_khan92', 'user_Reputation': 921.0, 'user_Views': 119.0, 'user_id': 5037278.0}}