【问题标题】:convert two columns into dictionary by category python-pandas按类别将两列转换为字典 python-pandas
【发布时间】:2021-02-18 23:33:52
【问题描述】:

我正在尝试将两列转换为字典,一列是类别,另一列是评论。

category comment
complain did not like his attention
complain I would never use this again
congrats you are the best
request I need my papers

想法是将其转换为按类别分组的字典,我的意思是这样的

Diccionario= {
    "complain":[
        'did not like his attention', 
        'I would never use this again'
    ],
    "congrats":[
        'you are the best'
    ],
    "request":[
        'I need my papers'
    ],
    
}

谢谢

【问题讨论】:

    标签: python pandas dataframe dictionary


    【解决方案1】:

    使用groupby.agg(list)to_dict

    df.groupby('category')['commet'].agg(list).to_dict()
    
    {'complain': ['did not like his attention', 'I would never use this again'],
     'congrats': ['you are the best'],
     'request': ['I need my papers']}
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2014-01-05
      • 2021-11-28
      • 1970-01-01
      • 2020-02-11
      • 2018-09-12
      相关资源
      最近更新 更多