首页 > 解决方案 > Pandas Python:附加数据框 - 文本

问题描述

假设我有以下数据框。如何在同一天附加与同一用户对应的行?Python-熊猫

user date text
A    1-1  how
A    1-1  are
A    3-1  the dog
B    1-2  hi
B    1-2  there
B    3-2  be good


user date text
A    1-1  how are
A    3-1  the dog
B    1-2  hi there
B    3-2  be good

标签: pythonpandas

解决方案


您可以使用以下功能pivot_table()

df.pivot_table(index=['user', 'date'], values='text', aggfunc=' '.join).reset_index()

结果:

  user date      text
0    A  1-1   how are
1    A  3-1   the dog
2    B  1-2  hi there
3    B  3-2   be good

推荐阅读