简体   繁体   中英

How to create a pivot table in pandas

I have a dataframe 'annual_stats' that I need to reshape into a pivot table. Attached you can see what it currently looks like and what it is supposed to look like after reshaping. what the dataframe is currently

what it needs to look like

Here are two possible ways to achieve this,

annual_stats.groupby(['state', 'year'])['salary'].sum().unstack()

pd.pivot_table(annual_stats, values='salary', index='state', columns='year', aggfunc='mean')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM