lolo
lolo

Reputation: 646

Pandas Python: append data frame - text

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

Answers (2)

Mykola Zotko
Mykola Zotko

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

Anton vBR
Anton vBR

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 of lambda 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

Related Questions