栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

熊猫:分组使用多个功能

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

熊猫:分组使用多个功能

您可以将

agg
列名作为键,将想要的函数作为值传递给字典。

import pandas as pdimport numpy as np# Create some randomised dataN = 20date_range = pd.date_range('01/01/2015', periods=N, freq='W')df = pd.Dataframe({'ages':np.arange(N), 'payments':np.arange(N)*10}, index=date_range)print(df.head())#  ages  payments# 2015-01-04     0         0# 2015-01-11     1        10# 2015-01-18     2        20# 2015-01-25     3        30# 2015-02-01     4        40# Apply np.mean to the ages column and np.sum to the payments.agg_funcs = {'ages':np.mean, 'payments':np.sum}# Groupby each individual month and then apply the funcs in agg_funcsgrouped = df.groupby(df.index.to_period('M')).agg(agg_funcs)print(grouped)#          ages  payments# 2015-01   1.5        60# 2015-02   5.5       220# 2015-03  10.0       500# 2015-04  14.5       580# 2015-05  18.0       540


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号