Python .map Nested Dataframe Dictionary
I have data nested in a dictionary that I would like to recode with another nested dictionary. Diagram here. The files dict is the location of the data I would like to recode based
Solution 1:
Just change the values of the dict df3_01, df3_11 to strings.
In your code:
# df3 are dicts nested within a dictionary (refs)
df3_01 = df3_0.set_index("ID").T.to_dict('list')
for key, value in df3_01.items():
df3_01[key]=value[0]
df3_11 = df3_1.set_index("ID").T.to_dict('list') # Convert df3 to dictfor key, value in df3_11.items():
df3_11[key]=value[0]
print(f'df3_01={df3_01}')
print(f'df3_11={df3_11}')
Output:
df3_01={'0': 'Select', '1': 'Male', '2': 'Female'}
df3_11={'0': 'Select',
'1': 'American Indian',
'2': 'Asian',
'3': 'Black or African American',
'4': 'Other'}enter code here
print('modified df')
print(df)
Output:
modified df
Log Gender Race Other
0 1114 Female American Indian 1
1 1115 Female Asian 4
2 1116 Female Black or African American 2
3 1117 NaN Other 1
4 1118 Male NaN 1
5 1119 Male Asian 3
6 120 Female Black or African American 2enter code here
Post a Comment for "Python .map Nested Dataframe Dictionary"