python的dataframe的groupby_python pandas.DataFrame.groupby()方法详解

本文详细介绍了Python Pandas库中DataFrame的groupby()方法,包括参数解析和使用示例。通过groupby()可以对DataFrame进行分组操作,便于对大量数据进行聚合计算。示例展示了如何根据日期字段对数据进行分组并计算平均值。
摘要由CSDN通过智能技术生成

文章目录

DataFrame.groupby()概览应用举例

概览内容参考自官方文档详情应用参考文档

DataFrame.groupby()概览

DataFrame.groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs)[source]Group DataFrame or Series using a mapper or by a Series of columns.A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.参数列表解析

by : mapping, function, label, or list of labels 映射关系, 函数, 标签或标签列表 Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method). If an ndarray is passed, the values are used as-is determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key.  axis : {0 or ‘index’, 1 or ‘columns’}, default 0 轴,0为横向的行, 1为竖向的列, 默认为0 Split along rows (0) or columns (1). 用于区分函数或方法是应用于行数据,还是列数据  level : int, level name, or sequence of such, default None 整型, 索引名, 默认是None If the axis is a MultiIndex (hierarchical), group by a particular level or levels. 多级索引的情况下, 函数或方法应用于哪一级的索引,  as_index : bool, default True 布尔型, 默认为True For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output.  sort : bool, default True 布尔型,是否排序, 默认为True Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group.  group_keys : bool, default True 布尔型, 默认为True, 当调用apply方法时, 添加group keys 到index用于识别. When calling apply, add group keys to index to identify pieces.  squeeze : bool, default False 布尔型,是否压缩, 默认为False, 可以理解为降维 Reduce the dimensionality of the return type if possible, otherwise return a consistent type.  observed : bool, default False 布尔型, 观察者, 默认为False This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.  New in version 0.23.0.  **kwargs Optional, only accepts keyword argument ‘mutated’ and is passed to groupby.

应用举例

talk is cheap, show me the code

In [1]: import pandas as pd

In [2]: import numpy as np

In [5]: df = pd.DataFrame([

...:     {'date': '2018-12-01', 'total': 105, 'total2': 133.03},

...:     {'date': '2018-12-01', 'total': 105, 'total2': 2032.13},

...:     {'date': '2018-12-01', 'total': 109, 'total2': 1312.32},

...:     {'date': '2018-12-01', 'total': 109, 'total2': 33120.23},

...:     {'date': '2018-12-02', 'total': 103, 'total2': 13123.23},

...:     {'date': '2018-12-02', 'total': 103, 'total2': 231232.13},

...:     {'date': '2018-12-02', 'total': 105, 'total2': 1310.32},

...:     {'date': '2018-12-03', 'total': 105, 'total2': 33403.23},

...:     {'date': '2018-12-03', 'total': 105, 'total2': 105.23},

...:     {'date': '2018-12-03', 'total': 102, 'total2': 23552.13},

...:     {'date': '2018-12-04', 'total': 102, 'total2': 10365.32},

...:     {'date': '2018-12-04', 'total': 106, 'total2': 30563.23},

...:     {'date': '2018-12-04', 'total': 106, 'total2': 130.33},

...:     {'date': '2018-12-04', 'total': 107, 'total2': 2042.13},

...:     {'date': '2018-12-03', 'total': 107, 'total2': 136.02},

...:     {'date': '2018-12-04', 'total': 107, 'total2': 3063.23},

...: ])

...:

In [8]: grouped = df.groupby(by=['date'])

In [12]: grouped.mean()

Out[12]:

total      total2

date

2018-12-01  107.000000   9149.4275

2018-12-02  103.666667  81888.5600

2018-12-03  104.750000  14299.1525

2018-12-04  105.600000   9232.8480

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值