Reputation: 646
suppose I have the following data frame. How can I append the rows corresponding to the same user on the same day? 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
Upvotes: 1
Views: 65
Reputation: 17834
You can use the function pivot_table()
:
df.pivot_table(index=['user', 'date'], values='text', aggfunc=' '.join).reset_index()
Result:
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
Upvotes: 1
Reputation: 18916
You are looking for a groupby and string join:
df.groupby(['user','date'])['text'].apply(' '.join).reset_index()
Note:
' '.join
is a shorter version oflambda x: ' '.join(x)
.
Full example:
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)
Returns:
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
Also look at this if it helps. The quick version to group all items in lists.
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]
Upvotes: 1