简体   繁体   English

如何在 pandas 中创建 pivot 表

[英]How to create a pivot table in pandas

I have a dataframe 'annual_stats' that I need to reshape into a pivot table.我有一个 dataframe 'annual_stats' ,我需要将其重塑为 pivot 表。 Attached you can see what it currently looks like and what it is supposed to look like after reshaping.附上你可以看到它现在的样子和重塑后的样子。 what the dataframe is currently dataframe 目前是什么

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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