简体   繁体   中英

pandas dataframe to nested dict

I have a dataframe like this:

    aa  phel  ri_s
no               
1   a    21    76
2   s    32    87
3   d    43    98
4   f    54    25
5   g    65    37

and I would like to create a dictionary that looks like this:

{1: {aa: a, phel: 21, ri_s: 76}, 2: {aa: s, phel: 32, ri_s:87}...}

but instead, I am getting this:

{'a': {(0, 'a'): {'phel': 21, 'ri_s': 76}}, 'd': {(2, 'd'): {'phel': 43, 'ri_s': 98}}, 'f': {(3, 'f'): {'phel': 54, 'ri_s': 25}}, 'g': {(4, 'g'): {'phel': 65, 'ri_s': 37}}, 's': {(1, 's'): {'phel': 32, 'ri_s': 87}}}

my current code is:

tsv_in = tsv_in.groupby('aa')['aa','phel', 'ri_s'].apply(
        lambda x: x.set_index('aa', 'phel', 'ri_s').to_dict(orient='index')).to_dict()

Any suggestions?

You can zip the index and the rows as dictionaries together, and run a dictionary comprehension:

{i:row for i,row in zip(df.index, df.to_dict(orient='row'))}

# returns
{1: {'aa': 'a', 'phel': 21, 'ri_s': 76},
 2: {'aa': 's', 'phel': 32, 'ri_s': 87},
 3: {'aa': 'd', 'phel': 43, 'ri_s': 98},
 4: {'aa': 'f', 'phel': 54, 'ri_s': 25},
 5: {'aa': 'g', 'phel': 65, 'ri_s': 37}}

as with many issues that arise when one is new to something, the answer was painfully simple. I was making it far too complicated.

All that was required to get the output I was looking for was:

df = df.to_dict("index")
print(df)

which returned:

{1: {'aa': 'a', 'phel': 21, 'ri_s': 76}, 2: {'aa': 's', 'phel': 32, 'ri_s': 87}, 3: {'aa': 'd', 'phel': 43, 'ri_s': 98}, 4: {'aa': 'f', 'phel': 54, 'ri_s': 25}, 5: {'aa': 'g', 'phel': 65, 'ri_s': 37}}

thanks to Psidom for the comment above.

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