Home > Back-end >  Pandas: Pivot with comma separated string aggregation?
Pandas: Pivot with comma separated string aggregation?

Time:03-18

How might I use comma separated string aggregation here as a form of a pivot table?

Consider this small example:

# mwe
df = pd.DataFrame({'property': ['DriveProductRevision', 'DriveProductRevision', 'DriveProductId', 'ProductNumber'],
                   'value': ['CXV84M1Q', 'CXV84M1Z', 'Samsung', 0]})

My attempt:

df.pivot_table(columns='property', values='value', aggfunc=lambda x: ', '.join(x))

The expected output:

DriveProductRevision    DriveProductId  ProductNumber
CXV84M1Q, CXV84M1Z         Samsung            0

CodePudding user response:

I'd actually to a groupby agg(list) .str.join:

df = df.assign(value=df['value'].astype(str)).groupby('property')['value'].agg(list).str.join(', ').to_frame().T.rename_axis(None, axis=1).reset_index(drop=True)

Output:

>>> df
  DriveProductId DriveProductRevision ProductNumber
0        Samsung   CXV84M1Q, CXV84M1Z             0

CodePudding user response:

You could adjust the lambda passed to aggfunc:

out = (df.pivot_table(columns='property', values='value', 
                      aggfunc=lambda x: x if len(x)==1 else ', '.join(x))
       .rename_axis(columns=[None]))

Output:

      DriveProductId DriveProductRevision ProductNumber
value        Samsung   CXV84M1Q, CXV84M1Z             0
  • Related