假设我有以下数据框架。如何在同一天追加对应于同一用户的行?Python-Pandas
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
发布于 2019-06-11 19:36:12
您正在寻找groupby和string联接:
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]
发布于 2019-06-11 19:59:26
您可以使用函数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
https://stackoverflow.com/questions/56550593
复制相似问题