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
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.