【python 分组累加求和】快速叫你分组求和和分组累加求和

# -*- encoding=utf-8 -*-
import pandas as pd
data=['abc','abc','abc','asc','ase','ase','ase']
num=[1,2,2,1,2,1,2]
df1=pd.DataFrame({'name':data,'num':num})
print(df1)

df1['mmm']=df1['num']
df2=df1.groupby(['name', 'num'], as_index=False).count()
print(df2)
df2.sort_values(['name', 'num'], ascending=[1, 1], inplace=True)
print(df2)
df2['sum']=df2.groupby(['name'])['mmm'].cumsum()
print(df2)
kk=df2.groupby(['name'],as_index=False)['num'].sum()
print(kk)
df3 = pd.merge(df2, kk, on='name', how='left',)
print(df3)
df3['ratio']=df3['sum']/df3['num_y']
df3.columns = ['name',  'num',  'mmm',  'sum','numsum','ratio']
print(df3)
df4=df3.groupby(['mmm'],as_index=False)['ratio'].mean()

print(df4)

运行:

  name  num
0  abc    1
1  abc    2
2  abc    2
3  asc    1
4  ase    2
5  ase    1
6  ase    2
  name  num  mmm
0  abc    1    1
1  abc    2    2
2  asc    1    1
3  ase    1    1
4  ase    2    2
  name  num  mmm
0  abc    1    1
1  abc    2    2
2  asc    1    1
3  ase    1    1
4  ase    2    2
  name  num  mmm  sum
0  abc    1    1    1
1  abc    2    2    3
2  asc    1    1    1
3  ase    1    1    1
4  ase    2    2    3
  name  num
0  abc    3
1  asc    1
2  ase    3
  name  num_x  mmm  sum  num_y
0  abc      1    1    1      3
1  abc      2    2    3      3
2  asc      1    1    1      1
3  ase      1    1    1      3
4  ase      2    2    3      3
  name  num  mmm  sum  numsum     ratio
0  abc    1    1    1       3  0.333333
1  abc    2    2    3       3  1.000000
2  asc    1    1    1       1  1.000000
3  ase    1    1    1       3  0.333333
4  ase    2    2    3       3  1.000000
   mmm     ratio
0    1  0.555556
1    2  1.000000

Process finished with exit code 0
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页