0

I have a dataframe that contains 2 columns, Class and Name. Here is an example of the data:

    Class       Name
    Physics     Anna
    Physics     Bob
    Maths       Gary
    Biology     Anna
    Biology     Gary

I am trying to join the 'Name' column based on the 'Class' column. The output I am looking for should be:

    Class       Name
    Physics     Anna Bob
    Maths       Gary
    Biology     Anna Gary

Note, no commas between the names. This is because I will be feeding this output to another system which requires the output to be in this format. Seeking assistance!

Thanks for reading!

Gary
  • 909
  • 8
  • 20
  • 1
    `df.goupby('Class')['Name'].agg(' '.join)` – Erfan Aug 21 '19 at 14:53
  • @Erfan: Thanks! This is almost what I am looking for. However, this code creates just one column with Class and aggregated Names. Is it possible to make it two columns, Class and Names with names joined ? – Gary Aug 21 '19 at 14:57
  • 1
    `df.groupby('Class', as_index=False).agg(' '.join)` or `df.goupby('Class')['Name'].agg(' '.join).reset_index()` – Erfan Aug 21 '19 at 14:59
  • This works. Thanks again @Erfan! – Gary Aug 21 '19 at 15:01

0 Answers0