pandas 分组统计

# coding:utf-8

import pandas as pd
import numpy as np

# path = r'C:\Users\wuzaipei\Desktop\签到.xlsx'
#
# data = pd.read_excel(path,header=0)

df=pd.DataFrame([[1,2,2],[1,4,5],[1,2,4],[1,6,3],[2,3,1],[2,4,1],[2,3,5],[3,1,1]],columns=['A','B','C'])
print(df)
gp=df.groupby(by=['A'])['B','C'].mean()  # 求均值
gs=df.groupby(by=['A'])['B','C'].sum()  # 求和

df1 = df.groupby('A').agg({'B': np.mean, 'C': np.sum}) # 求均值和求和

# gp.size()
print(type(gp))
print(gp)
print(df1)

  

posted @ 2019-10-12 22:57  洺剑残虹  阅读(1006)  评论(0编辑  收藏  举报