1

I want to create a dictionary out of my data frame. Data frame look like this:

Vehical | Brand  | Model Name
car     | Suzuki | abc 
car     | Honda  | def
bike    | Suzuki | xyz
bike    | Honda  | asd

I want my dictionary like:

{car : {Suzuki : abc, Honda : def}, bike : {Suzuki : xyz, Honda : asd}}
0

1 Answer 1

3

you can do it this way:

In [29]: df.pivot(index='Vehical', columns='Brand', values='Model Name').to_dict('i')
Out[29]:
{'bike': {'Honda': 'asd', 'Suzuki': 'xyz'},
 'car': {'Honda': 'def', 'Suzuki': 'abc'}}

result of pivoting:

In [28]: df.pivot(index='Vehical', columns='Brand', values='Model Name')
Out[28]:
Brand   Honda Suzuki
Vehical
bike      asd    xyz
car       def    abc
Sign up to request clarification or add additional context in comments.

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.