【问题标题】:Convert Pandas Dataframe to_dict() with unique column values as keys使用唯一列值作为键将 Pandas Dataframe 转换为 to_dict()
【发布时间】:2019-06-05 13:22:23
【问题描述】:

如何使用唯一列值作为字典的键将 pandas 数据帧转换为字典?在这种情况下,我想使用唯一的用户名作为键。

这是我目前根据在此处和在线上找到的信息所取得的进展。

我的测试数据框:

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']
})

我可以这样创建字典:

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'}]

我尝试使用 groupbyapply 方法让我更接近,但它会将所有值转换为列表。我希望它们保留为字典,以便我可以保留每个值的键:

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']]}

这是我想要的结果:

{   
    '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'}]
}

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我更喜欢在这里使用 for 循环,你也可能想要 drop username 列,因为它是多余的

    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'}]}
    

    【讨论】:

    • 对你们俩来说都是很好的答案。放弃它可能是个好主意
    【解决方案2】:

    使用groupbyapply。在应用程序中,使用“记录”方向调用to_dict(类似于您已经弄清楚的)。

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

    【讨论】:

    • 我觉得我很接近。谢谢你帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多