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