Issue
I have a pandas dataframe and it looks like so:
person weight height skill
kate 160 200 100
john 170 150 70
I have set the person column as the index of my python dataframe. And then i turned it into a dictionary using the .to_dict()
method. this gives the following dictionary:
{'weight': {'kate': '160', 'john': '170'},
'height': {'kate': '200', 'john': '150'},
'skill': {'kate': '100', 'john': '70'},
I need to change the around so that the dictionary is per person, but i dont know if its possible to do in python. What i need my dictionary to look like is:
{'Kate: { weight': '160', 'height': '200', 'skill': '100'},
'John': {'weight': '170', 'height': '150', 'skill': '70'}}
Is it possible to do this?
Solution
You can set person
as the index and use to_dict
with orient
arg set to index
.
df.set_index('person').to_dict('index')
# {'kate': {'weight': 160, 'height': 200, 'skill': 100},
# 'john': {'weight': 170, 'height': 150, 'skill': 70}}
Answered By - Ch3steR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.