1

I want to combine the rows and separate distinct entries by a comma. I tried the following. Starting from

    Postcode    Borough         Neighbourhood
    M3A         North York      Parkwoods
    M3A         North York      Victoria Village

I typed in the following command

df.groupby(['Postcode','Borough'])["Neighbourhood"].apply(lambda item:', '.join(item)

But that gives me

                                            Neighbourhood
            Postcode    Borough         
            M3A         North York          Parkwoods, Victoria Village

The problem is that the last column is somehow 'above' all the others. Can''t I do this in a way that retains the old column structure? Thanks!

yishairasowsky
  • 741
  • 1
  • 7
  • 21
  • Use `reset_index` after, or specify `as_index=False` in `groupby`. – cs95 May 16 '19 at 18:26
  • i tried doing ```as_index=False``` in ```groupby```, but i got almost the same result, the only difference being that now over the last column it says ```0``` instead of ```Neighborhoods```. Any thoughts? Thanks! – yishairasowsky May 18 '19 at 18:24
  • I suggest using your existing code, but use reset_index(name="Neighbourhood") after that. – cs95 May 18 '19 at 18:32

0 Answers0