第3章 分组

groupby函数

分组函数的基本内容:

根据某一列分组
根据某几列分组
组容量与组数
组的遍历

for name,group in grouped_single:
    print(name)
    display(group.head())

level参数(用于多级索引)和axis参数

df.set_index(['Gender','School']).groupby(level=1,axis=0).get_group('S_1').head()

groupby的对象:

[‘Address’, ‘Class’, ‘Gender’, ‘Height’, ‘Math’, ‘Physics’, ‘School’, ‘Weight’, ‘agg’, ‘aggregate’, ‘all’, ‘any’, ‘apply’, ‘backfill’, ‘bfill’, ‘boxplot’, ‘corr’, ‘corrwith’, ‘count’, ‘cov’, ‘cumcount’, ‘cummax’, ‘cummin’, ‘cumprod’, ‘cumsum’, ‘describe’, ‘diff’, ‘dtypes’, ‘expanding’, ‘ffill’, ‘fillna’, ‘filter’, ‘first’, ‘get_group’, ‘groups’, ‘head’, ‘hist’, ‘idxmax’, ‘idxmin’, ‘indices’, ‘last’, ‘mad’, ‘max’, ‘mean’, ‘median’, ‘min’, ‘ndim’, ‘ngroup’, ‘ngroups’, ‘nth’, ‘nunique’, ‘ohlc’, ‘pad’, ‘pct_change’, ‘pipe’, ‘plot’, ‘prod’, ‘quantile’, ‘rank’, ‘resample’, ‘rolling’, ‘sem’, ‘shift’, ‘size’, ‘skew’, ‘std’, ‘sum’, ‘tail’, ‘take’, ‘transform’, ‘tshift’, ‘var’]

聚合、过滤和变换

常用聚合函数
group_m = grouped_single[‘Math’]
group_m.std().values/np.sqrt(group_m.count().values)== group_m.sem().values

同时使用多个聚合函数

过滤(Filteration)
变换(Transformation)

apply函数

apply函数的灵活性
用apply同时统计多个指标

‘’’
from collections import OrderedDict
def f(df):
data = OrderedDict()
data[‘M_sum’] = df[‘Math’].sum()
data[‘W_var’] = df[‘Weight’].var()
data[‘H_mean’] = df[‘Height’].mean()
return pd.Series(data)
‘’’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值