简体   繁体   中英

Pandas: Pivot with comma separated string aggregation?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM