Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.pivot_table#

pandas.pivot_table(data,values=None,index=None,columns=None,aggfunc='mean',fill_value=None,margins=False,dropna=True,margins_name='All',observed=<no_default>,sort=True)[source]#

Create a spreadsheet-style pivot table as a DataFrame.

The levels in the pivot table will be stored in MultiIndex objects(hierarchical indexes) on the index and columns of the result DataFrame.

Parameters:
dataDataFrame
valueslist-like or scalar, optional

Column or columns to aggregate.

indexcolumn, Grouper, array, or list of the previous

Keys to group by on the pivot table index. If a list is passed,it can contain any of the other types (except list). If an array ispassed, it must be the same length as the data and will be used inthe same manner as column values.

columnscolumn, Grouper, array, or list of the previous

Keys to group by on the pivot table column. If a list is passed,it can contain any of the other types (except list). If an array ispassed, it must be the same length as the data and will be used inthe same manner as column values.

aggfuncfunction, list of functions, dict, default “mean”

If a list of functions is passed, the resulting pivot table will havehierarchical columns whose top level are the function names(inferred from the function objects themselves).If a dict is passed, the key is column to aggregate and the value isfunction or list of functions. Ifmargin=True, aggfunc will beused to calculate the partial aggregates.

fill_valuescalar, default None

Value to replace missing values with (in the resulting pivot table,after aggregation).

marginsbool, default False

Ifmargins=True, specialAll columns and rowswill be added with partial group aggregates across the categorieson the rows and columns.

dropnabool, default True

Do not include columns whose entries are all NaN. If True,rows with a NaN value in any column will be omitted beforecomputing margins.

margins_namestr, default ‘All’

Name of the row / column that will contain the totalswhen margins is True.

observedbool, default 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.

Deprecated since version 2.2.0:The default value ofFalse is deprecated and will change toTrue in a future version of pandas.

sortbool, default True

Specifies if the result should be sorted.

Added in version 1.3.0.

Returns:
DataFrame

An Excel style pivot table.

See also

DataFrame.pivot

Pivot without aggregation that can handle non-numeric data.

DataFrame.melt

Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

wide_to_long

Wide panel to long format. Less flexible but more user-friendly than melt.

Notes

Referencethe user guide for more examples.

Examples

>>>df=pd.DataFrame({"A":["foo","foo","foo","foo","foo",..."bar","bar","bar","bar"],..."B":["one","one","one","two","two",..."one","one","two","two"],..."C":["small","large","large","small",..."small","large","small","small",..."large"],..."D":[1,2,2,3,3,4,5,6,7],..."E":[2,4,5,5,6,6,8,9,9]})>>>df     A    B      C  D  E0  foo  one  small  1  21  foo  one  large  2  42  foo  one  large  2  53  foo  two  small  3  54  foo  two  small  3  65  bar  one  large  4  66  bar  one  small  5  87  bar  two  small  6  98  bar  two  large  7  9

This first example aggregates values by taking the sum.

>>>table=pd.pivot_table(df,values='D',index=['A','B'],...columns=['C'],aggfunc="sum")>>>tableC        large  smallA   Bbar one    4.0    5.0    two    7.0    6.0foo one    4.0    1.0    two    NaN    6.0

We can also fill missing values using thefill_value parameter.

>>>table=pd.pivot_table(df,values='D',index=['A','B'],...columns=['C'],aggfunc="sum",fill_value=0)>>>tableC        large  smallA   Bbar one      4      5    two      7      6foo one      4      1    two      0      6

The next example aggregates by taking the mean across multiple columns.

>>>table=pd.pivot_table(df,values=['D','E'],index=['A','C'],...aggfunc={'D':"mean",'E':"mean"})>>>table                D         EA   Cbar large  5.500000  7.500000    small  5.500000  8.500000foo large  2.000000  4.500000    small  2.333333  4.333333

We can also calculate multiple types of aggregations for any givenvalue column.

>>>table=pd.pivot_table(df,values=['D','E'],index=['A','C'],...aggfunc={'D':"mean",...'E':["min","max","mean"]})>>>table                  D   E               mean max      mean  minA   Cbar large  5.500000   9  7.500000    6    small  5.500000   9  8.500000    8foo large  2.000000   5  4.500000    4    small  2.333333   6  4.333333    2

[8]ページ先頭

©2009-2025 Movatter.jp