Shogun187
Shogun187

Reputation: 88

Pandas GroupBy Join Strings Multiple Columns

Could somebody tell me how to simplify this piece of code? It is archaic to write all the column names, specially on wide tables. For some reason if I run df.groupby('APID').agg(','.join) it does not return all the columns. Only the first 3. All the columns are of type 'object'.

df.groupby('APID').agg(
    {'Names':','.join,
     'Alias_Name':','.join,
     'APID':','.join,
     'Prev_Rep':','.join,
     'Rep_Assigned':','.join,
     'City':','.join,
     'State':','.join,
     'Zip_Code':','.join,
     'Country':','.join,
     'Distribution_Numbers':','.join,
     'Partnership':','.join,
     'Onboarding':','.join,}
)

Upvotes: 0

Views: 717

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34086

Just this:

df.groupby('APID').apply(','.join)

Upvotes: 1

Related Questions