python pandas.DataFrame.groupby()方法详解

文章目录

  • 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


你可能感兴趣的:(python.pandas,进阶)