I have a pandas multiindex dataframe that I'm trying to output as a nested dictionary.
# create the dataset
data = {'clump_thickness': {(0, 0): 274.0, (0, 1): 19.0, (1, 0): 67.0, (1, 1): 12.0, (2, 0): 83.0, (2, 1): 45.0, (3, 0): 16.0, (3, 1): 40.0, (4, 0): 4.0, (4, 1): 54.0, (5, 0): 0.0, (5, 1): 69.0, (6, 0): 0.0, (6, 1): 0.0, (7, 0): 0.0, (7, 1): 0.0, (8, 0): 0.0, (8, 1): 0.0, (9, 0): 0.0, (9, 1): 0.0}}
df = pd.DataFrame(data)
df.head()
# clump_thickness
# 0 0 274.0
# 1 19.0
# 1 0 67.0
# 1 12.0
# 2 0 83.0
df
is the dataframe that I want to output as a nested dictionary. The output I'm looking for is in the form -
{"0":
{
"0":274,
"1":19
},
"1":{
"0":67,
"1":12
},
"2":{
"0":83,
"1":45
},
"3":{
"0":16,
"1":40
},
"4":{
"0":4,
"1":54
},
"5":{
"0":0,
"1":69
}
}
Here the first index forms the keys of the outer most dictionary. For each key we have a dictionary stored whose keys are the values in the second index.
When I do df.to_dict()
, the instead of nesting, the multiindex is returned as a tuple. How do I achieve this?
For me working:
d = {l: df.xs(l)['clump_thickness'].to_dict() for l in df.index.levels[0]}
Another solution similar like DataFrame with MultiIndex to dict , but is necessary filter column for Series
:
d = df.groupby(level=0).apply(lambda df: df.xs(df.name).clump_thickness.to_dict()).to_dict()
print (d)
{0: {0: 274.0, 1: 19.0},
1: {0: 67.0, 1: 12.0},
2: {0: 83.0, 1: 45.0},
3: {0: 16.0, 1: 40.0},
4: {0: 4.0, 1: 54.0},
5: {0: 0.0, 1: 69.0},
6: {0: 0.0, 1: 0.0},
7: {0: 0.0, 1: 0.0},
8: {0: 0.0, 1: 0.0},
9: {0: 0.0, 1: 0.0}}
df.unstack().clump_thickness.apply(lambda x: x.to_dict(), axis=1).to_dict()
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.