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
  • 56,955
  • 33
  • 144
  • 158
user2355903
  • 593
  • 2
  • 8
  • 29

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
  • 146,074
  • 10
  • 56
  • 74
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
  • 317,841
  • 20
  • 164
  • 234