MCPcopy Create free account
hub / github.com/dask/dask / pivot_table

Function pivot_table

dask/dataframe/dask_expr/_collection.py:6072–6124  ·  view source on GitHub ↗

Create a spreadsheet-style pivot table as a DataFrame. Target ``columns`` must have category dtype to infer result's ``columns``. ``index``, ``columns``, and ``aggfunc`` must be all scalar. ``values`` can be scalar or list-like. Parameters ---------- df : DataFrame

(df, index, columns, values, aggfunc="mean")

Source from the content-addressed store, hash-verified

6070
6071
6072def pivot_table(df, index, columns, values, aggfunc="mean"):
6073 """
6074 Create a spreadsheet-style pivot table as a DataFrame. Target ``columns``
6075 must have category dtype to infer result's ``columns``.
6076 ``index``, ``columns``, and ``aggfunc`` must be all scalar.
6077 ``values`` can be scalar or list-like.
6078
6079 Parameters
6080 ----------
6081 df : DataFrame
6082 index : scalar
6083 column to be index
6084 columns : scalar
6085 column to be columns
6086 values : scalar or list(scalar)
6087 column(s) to aggregate
6088 aggfunc : {'mean', 'sum', 'count', 'first', 'last'}, default 'mean'
6089
6090 Returns
6091 -------
6092 table : DataFrame
6093
6094 See Also
6095 --------
6096 pandas.DataFrame.pivot_table
6097 """
6098 if not is_scalar(index) or index not in df._meta.columns:
6099 raise ValueError("'index' must be the name of an existing column")
6100 if not is_scalar(columns) or columns not in df._meta.columns:
6101 raise ValueError("'columns' must be the name of an existing column")
6102 if not methods.is_categorical_dtype(df._meta[columns]):
6103 raise ValueError("'columns' must be category dtype")
6104 if not has_known_categories(df._meta[columns]):
6105 raise ValueError("'columns' must have known categories")
6106
6107 if not (
6108 is_scalar(values)
6109 and values in df._meta.columns
6110 or not is_scalar(values)
6111 and all(is_scalar(x) and x in df._meta.columns for x in values)
6112 ):
6113 raise ValueError("'values' must refer to an existing column or columns")
6114
6115 available_aggfuncs = ["mean", "sum", "count", "first", "last"]
6116
6117 if not is_scalar(aggfunc) or aggfunc not in available_aggfuncs:
6118 raise ValueError(
6119 "aggfunc must be either " + ", ".join(f"'{x}'" for x in available_aggfuncs)
6120 )
6121
6122 return new_collection(
6123 PivotTable(df, index=index, columns=columns, values=values, aggfunc=aggfunc)
6124 )
6125
6126
6127@derived_from(pd, ua_args=["downcast"])

Callers 3

test_pivot_tableFunction · 0.90
test_pivot_table_failsFunction · 0.90
pivot_tableMethod · 0.85

Calls 6

is_scalarFunction · 0.90
has_known_categoriesFunction · 0.90
new_collectionFunction · 0.90
PivotTableClass · 0.90
allFunction · 0.85
joinMethod · 0.45

Tested by 2

test_pivot_tableFunction · 0.72
test_pivot_table_failsFunction · 0.72