python pandas group by and aggregate columns

牧云@^-^@ 提交于 2020-01-25 02:59:24

问题


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?


回答1:


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 bellow
#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  


来源:https://stackoverflow.com/questions/52663402/python-pandas-group-by-and-aggregate-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!