简体   繁体   中英

Convert Pandas Dataframe to_dict() with unique column values as keys

How can I convert a pandas dataframe to a dict using unique column values as the keys for the dictionary? In this case I want to use unique username's as the key.

Here is my progress so far based on information found on here and online.

My test dataframe:

import pandas
import pprint

df = pandas.DataFrame({
    'username': ['Kevin', 'John', 'Kevin', 'John', 'Leslie', 'John'], 
    'sport': ['Soccer', 'Football', 'Racing', 'Tennis', 'Baseball', 'Bowling'],
    'age': ['51','32','20','19','34','27'],
    'team': ['Cowboyws', 'Packers', 'Sonics', 'Raiders', 'Wolves', 'Lakers']
})

I can create a dictionary by doing this:

dct = df.to_dict(orient='records')
pprint.pprint(dct, indent=4)

>>>>[{'age': '51', 'sport': 'Soccer', 'team': 'Cowboyws', 'username': 'Kevin'},
    {'age': '32', 'sport': 'Football', 'team': 'Packers', 'username': 'John'},
    {'age': '20', 'sport': 'Racing', 'team': 'Sonics', 'username': 'Kevin'},
    {'age': '19', 'sport': 'Tennis', 'team': 'Raiders', 'username': 'John'},
    {'age': '34', 'sport': 'Baseball', 'team': 'Wolves', 'username': 'Leslie'},
    {'age': '27', 'sport': 'Bowling', 'team': 'Lakers', 'username': 'John'}]

I tried using the groupby and apply method which got me closer but it converts all the values to lists. I want them to remain as dictionaries so i can retain the each value's key:

result = df.groupby('username').apply(lambda x: x.values.tolist()).to_dict()
pprint.pprint(result, indent=4)

{   'John': [   ['32', 'Football', 'Packers', 'John'],
                ['19', 'Tennis', 'Raiders', 'John'],
                ['27', 'Bowling', 'Lakers', 'John']],
    'Kevin': [   ['51', 'Soccer', 'Cowboyws', 'Kevin'],
                 ['20', 'Racing', 'Sonics', 'Kevin']],
    'Leslie': [['34', 'Baseball', 'Wolves', 'Leslie']]}

This is the desired result I want:

{   
    'John': [{'age': '32', 'sport': 'Football', 'team': 'Packers', 'username': 'John'},
             {'age': '19', 'sport': 'Tennis', 'team': 'Raiders', 'username': 'John'},
             {'age': '27', 'sport': 'Bowling', 'team': 'Lakers', 'username': 'John'}],
    'Kevin': [{'age': '51', 'sport': 'Soccer', 'team': 'Cowboyws', 'username': 'Kevin'},
              {'age': '20', 'sport': 'Racing', 'team': 'Sonics', 'username': 'Kevin'}],
    'Leslie': [{'age': '34', 'sport': 'Baseball', 'team': 'Wolves', 'username': 'Leslie'}]
}

Use groupby and apply . Inside the apply, call to_dict with the "records" orient (similar to what you've figured out already).

df.groupby('username').apply(lambda x: x.to_dict(orient='r')).to_dict()

I prefer using for loop here , also you may want to drop the username columns , since it is redundant

d = {x: y.drop('username',1).to_dict('r') for x , y in df.groupby('username')}
d
Out[212]: 
{'John': [{'age': '32', 'sport': 'Football', 'team': 'Packers'},
  {'age': '19', 'sport': 'Tennis', 'team': 'Raiders'},
  {'age': '27', 'sport': 'Bowling', 'team': 'Lakers'}],
 'Kevin': [{'age': '51', 'sport': 'Soccer', 'team': 'Cowboyws'},
  {'age': '20', 'sport': 'Racing', 'team': 'Sonics'}],
 'Leslie': [{'age': '34', 'sport': 'Baseball', 'team': 'Wolves'}]}

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