A pivot is a transformation that takes a dataframe with columns representing categories and rows representing values, and reorients it so that the categories are in the rows, the values are in the columns, and the index is set to the original row values.
Basic syntax:
df.pivot(index=<row_labels>, columns=<col_labels>, values=<value_cols>)
Examples:
df.pivot(index='row', columns='col', values='val')
df.pivot(index=['row', 'item'], columns='col', values='val')
df.pivot(index='row', columns='col', values=['val0', 'val1'])
df.pivot(index='row', columns='col', values='val', aggfunc='mean')
By default, if there are duplicate keys in the row or column labels, an error will be raised. Alternatively, you can use:
df.pivot_table(index='row', columns='col', values='val', fill_value=0)
groupby unstack:
df.groupby('row', 'col')['val'].mean().unstack(fill_value=0)
pd.crosstab(index=df['row'], columns=df['col'], values=df['val'], aggfunc='count')
df.pivot_table(index='row', columns='col', values='val', aggfunc=['mean', 'sum'])
df.pivot_table(index='row', columns=['item', 'col'], values='val', fill_value=0, aggfunc='mean')
The above is the detailed content of How Can I Effectively Pivot a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!