MulDataFrame.groupby#
- MulDataFrame.groupby(by=None, axis=0, agg_mode: Literal['same_only'] | Literal['list'] | Literal['tuple'] = 'same_only', keep_primary=False, **kwargs)#
Group MulDataFrame by its index or columns dataframe using a mapper or the index/columns dataframe’s columns.
The function uses the DataFrame.groupby method of the index or columns dataframe to create groups under the hood. The values of the MulDataFrame are grouped accordingly. It returns a MulGroupBy object that contains information about the groups.
Parameters#
- byNone, mapping, function, label, pd.Grouper or list of such
Please refers to DataFrame.groupby for detailed information on this argument. Its difference to the
byargument inDataFrame.groupbyis that if it is None, uses the primary index (ifaxis==0) or the primary columns (ifaxis==1) to group the MulDataFrame.- axis0 or 1, default 0
Whether to group the MulDataFrame by its index dataframe (if
axis==0) or by its columns dataframe (ifaxis==1).- agg_mode‘same_only’, ‘list’,’tuple’, default to ‘same only’
Determine how to aggregate column values in the index or columns dataframe that are not the same in each group when calling numpy functions on or using the call method of the MulGroupBy object.
'same_only': only keep columns that have the identical values in each group.'list': put columns that do not have the identical values in a group into a list.'tuple': similar to ‘list’, but put them into a tuple.
- keep_primarybool, default False
Whether to keep primary index or columns in the index (
axis=0) or columns (axis=1) dataframe in each group. IfTrue, the primary index or columns will be reset as a column and kept in the index or columns dataframe in each group. If the name of the primary index or columns isNone,"primary_index"will be used as its name.
Returns#
- MulGroupBy
A MulGroupBy object that contains information about the groups.
Examples#
>>> mf (3, 2) g 7 6 f 5 3 c d -------- --------- x y c d a 1 2 a 1 2 b 3 6 b 8 9 b 5 6 b 8 10 >>> for key, group in mf.groupby('y'): ... if key == 6: ... print(key,'\n',group) 6 (2, 2) g 7 6 f 5 3 c d -------- --------- x y c d b 3 6 b 8 9 b 5 6 b 8 10 >>> mf.groupby('y').mean() (2, 2) g 7 6 f 5 3 c d --------------- ----------- Empty DataFrame c d Columns: [] y Index: [2, 6] 2 1.0 2.0 6 8.0 9.5 >>> mf.groupby('y',agg_mode='list',keep_primary=True).mean() (2, 2) g 7 6 f 5 3 c d ----------------------- ----------- primary_index x c d y y 2 a 1 2 1.0 2.0 6 b [3, 5] 6 8.0 9.5
MulDataFrame