python-3.x - python 3 . x - 将数据框中的值转换为列表

59 2

我有一个来自Dota中已解析的数据帧,其中包含match_id.slot和text列的聊天信息,每行代表一行文本,是否可以直接执行还是必须从头开始创建新的数据框?

下面是一个示例输入:

match_id , slot , text
0 , 0 , "gg "
0 , 2 , "good game "
0 , 2 , "well played "
1 , 0 , "glhf "
1 , 6 , "u2 "
1 , 0 , "thx "

我想把它汇总成:

match_id , slot , text
0 , 0 , "gg "
0 , 2 , {"good game " , " well played"}
1 , 0 , {"glhf " , " thx"}
1 , 6 , "u2 "

时间: 原作者:

141 3

使用这个代码,


import pandas as pd


df = pd.DataFrame({'match_id' :[0,0,0,1,1,1] ,'slot': [0,2,2,0,6,0] ,'text': ['gg','good game','well played' ,'glff' , 'u2' , 'thx'] })


df.groupby(['match_id','slot'])['text'].apply(list).reset_index(name='text_list')



输出:


 match_id slot text_list


0 0 0 [gg]


1 0 2 [game, well played]


2 1 0 [glff, thx]


3 1 6 [u2]



原作者:
...