假设我有以下数据框。如何在同一天附加与同一用户对应的行? 蟒蛇熊猫
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
答案 0 :(得分:1)
您正在寻找groupby和字符串连接:
df.groupby(['user','date'])['text'].apply(' '.join).reset_index()
注意:
' '.join
是lambda x: ' '.join(x)
的缩写。
完整示例:
import pandas as pd
data = '''\
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'''
fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep=',')
df = df.groupby(['user','date'])['text'].apply(' '.join).reset_index()
print(df)
返回:
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
如果有帮助,也请看一下。用于将列表中所有项目分组的快速版本。
print(df.groupby(['user','date'])['text'].apply(list).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]
答案 1 :(得分:1)
您可以使用函数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