简体   繁体   中英

How do you “pivot” using conditions, aggregation, and concatenation in Pandas?

I have a dataframe in a format such as the following:

Index    Name    Fruit           Quantity
0        John    Apple Red       10
1        John    Apple Green      5
2        John    Orange Cali     12
3        Jane    Apple Red       10
4        Jane    Apple Green      5
5        Jane    Orange Cali     18
6        Jane    Orange Spain     2

I need to turn it into a dataframe such as this:

Index    Name    All Fruits                                         Apples Total  Oranges Total
0        John    Apple Red, Apple Green, Orange Cali                          15             12
1        Jane    Apple Red, Apple Green, Orange Cali, Orange Spain            15             20

Question is how do I do this? I have looked at the groupby docs as well as a number of posts on pivot and aggregation but translating that into this use case somehow escapes me. Any help or pointers much appreciated.

Cheers!

Use GroupBy.agg with join , create column F by split and pass to DataFrame.pivot_table , last join together by DataFrame.join :

df1 = df.groupby('Name', sort=False)['Fruit'].agg(', '.join)
df2 = (df.assign(F = df['Fruit'].str.split().str[0])
        .pivot_table(index='Name', 
                     columns='F', 
                     values='Quantity',
                     aggfunc='sum')
        .add_suffix(' Total'))


df3 = df1.to_frame('All Fruits').join(df2).reset_index()
print (df3)
   Name                                         All Fruits  Apple Total  \
0  John                Apple Red, Apple Green, Orange Cali           15   
1  Jane  Apple Red, Apple Green, Orange Cali, Orange Spain           15   

   Orange Total  
0            12  
1            20  

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