0

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?

3
  • 3
    df.to_dict("index")? Commented Apr 20, 2017 at 2:09
  • my god, it really was this simple. completely new to pandas, I suppose this is painfully obvious...Thank you! Commented Apr 20, 2017 at 2:29
  • Many thanks - that's not obvious however. Commented May 22, 2017 at 8:06

2 Answers 2

2

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}}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.