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=, columns= , values= )
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')
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3