简体   繁体   中英

python pandas group by and aggregate columns

I am using panda version 0.23.0. I want to use data frame group by function to generate new aggregated columns using [lambda] functions..

My data frame looks like

ID Flag Amount User
 1  1    100    123345
 1  1    55     123346
 2  0    20     123346
 2  0    30     123347
 3  0    50     123348

I want to generate a table which looks like

ID Flag0_Count Flag1_Count  Flag0_Amount_SUM    Flag1_Amount_SUM  Flag0_User_Count Flag1_User_Count
 1  2           2           0                   155                0                2
 2  2           0           50                  0                  2                0
 3  1           0           50                  0                  1                0

here:

  1. Flag0_Count is count of Flag = 0
  2. Flag1_Count is count of Flag = 1
  3. Flag0_Amount_SUM is SUNM of amount when Flag = 0
  4. Flag1_Amount_SUM is SUNM of amount when Flag = 1
  5. Flag0_User_Count is Count of Distinct User when Flag = 0
  6. Flag1_User_Count is Count of Distinct User when Flag = 1

I have tried something like

df.groupby(["ID"])["Flag"].apply(lambda x: sum(x==0)).reset_index()

but it creates a new a new data frame. This means I will have to this for all columns and them merge them together into a new data frame. Is there an easier way to accomplish this?

Use DataFrameGroupBy.agg by dictionary by column names with aggregate function, then reshape by unstack , flatten MultiIndex of columns, rename columns and last reset_index :

df = (df.groupby(["ID", "Flag"])
      .agg({'Flag':'size', 'Amount':'sum', 'User':'nunique'})
      .unstack(fill_value=0))

#python 3.6+
df.columns = [f'{i}{j}' for i, j in df.columns]
#python below
#df.columns = [f'{}{}'.format(i, j) for i, j in df.columns]
d = {'Flag0':'Flag0_Count',
     'Flag1':'Flag1_Count',
     'Amount0':'Flag0_Amount_SUM',
     'Amount1':'Flag1_Amount_SUM',
     'User0':'Flag0_User_Count',
     'User1':'Flag1_User_Count',
     }
df = df.rename(columns=d).reset_index()
print (df)

   ID  Flag0_Count  Flag1_Count  Flag0_Amount_SUM  Flag1_Amount_SUM  \
0   1            0            2                 0               155   
1   2            2            0                50                 0   
2   3            1            0                50                 0   

   Flag0_User_Count  Flag1_User_Count  
0                 0                 2  
1                 2                 0  
2                 1                 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