dataframe 行转列

two_level_index_series = df.set_index(["姓名", "科目"])["分数"]
new_df = two_level_index_series.unstack()
new_df = new_df.reset_index()

三个index同样可以生效

df_groupby = df.groupby(['a','b','c'])['count'].sum()
df_groupby = df_groupby.unstack().reset_index()

你可能感兴趣的:(dataframe 行转列)