1

I have a dataframe as follows:

A B C 
1 6 1 
2 5 7 
3 4 9
4 2 2

I want a dictionary like this:

{A: [1,2,3,4], B:[6,5,4,2], C:[1,7,9,2]}

I have tried using the normal df.to_dict() and it is no where close. If I use the transposed dataframe, hence df.T.to_dict() it gets close, but I have something like this:

{0: {A: 1, B: 6, C:1} , ... , 4:{A: 4, B: 2, C: 2 } }

The questions in stack overflow are limited to the dictionary having one value per key, not an array.

It would be very valuable for me to use to_dict() and avoid any for loop, since the database I am using is quite big and I want the computational complexity to be as low as possible.

4
  • 3
    df.to_dict(orient='list') or df.to_dict('l') Commented Jun 25, 2019 at 21:22
  • Try playing with the orient= keyword in to_dict Commented Jun 25, 2019 at 21:23
  • @piRSquared It worked, you should put it as an official answer. Commented Jun 25, 2019 at 21:25
  • By the way, would it be possible to preserve it as a numpy array and not just a list? Commented Jun 25, 2019 at 21:27

1 Answer 1

5

orient='list'

df.to_dict(orient='list')

Or: the method actually just checks the first character

df.to_dict('l')

If you want to preserve the numpy array

{k: v.to_numpy() for k, v in df.items()}
Sign up to request clarification or add additional context in comments.

2 Comments

Would the computational cost increase a lot if you preserve it as a numpy array?
Not really. That should be pretty snappy.

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.