0

I have a dataframe structured like this:

df_have = pd.DataFrame({'id':[1,1,2,3,4,4], 'desc':['yes','no','chair','bird','person','car']})

How can I get something like this:

df_want = pd.DataFrame({'id':[1,2,3,4], 'desc':['yes no','chair','bird','person car']})
Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
user2355903
  • 567
  • 1
  • 5
  • 21

2 Answers2

2

Use groupby().apply:

df_have.groupby('id', as_index=False)['desc'].apply(' '.join)

Output:

   id        desc
0   1      yes no
1   2       chair
2   3        bird
3   4  person car
Quang Hoang
  • 131,600
  • 10
  • 43
  • 63
1

I will do agg with groupby

df = df_have.groupby('id',as_index=False)[['desc']].agg(' '.join)
   id        desc
0   1      yes no
1   2       chair
2   3        bird
3   4  person car
BENY
  • 296,997
  • 19
  • 147
  • 204