格式化分组数据框
Formatting Grouped Dataframe
我正在阅读以下关于分组依据的教程,https://chrisalbon.com/python/pandas_apply_operations_to_groups.html。
在下面概述的分组之后,我如何才能转换为仅包含 团、第 1、第 2 列的数据框?
我认为你试图从数据框中删除那家公司,所以使用
df.columns.name = ''
如果您想重置索引,请使用
df = df.reset_index()
输出:
ndf = df['preTestScore'].groupby([df['regiment'], df['company']]).mean().unstack()
ndf.columns.name = ''
1st 2nd
regiment
Dragoons 3.5 27.5
Nighthawks 14.0 16.5
Scouts 2.5 2.5
To have dataframe simply has regiment, 1st, 2nd as its columns
ndf = ndf.reset_index()
regiment 1st 2nd
0 Dragoons 3.5 27.5
1 Nighthawks 14.0 16.5
2 Scouts 2.5 2.5
通过使用rename_axis
df.rename_axis(None,1)
我正在阅读以下关于分组依据的教程,https://chrisalbon.com/python/pandas_apply_operations_to_groups.html。
在下面概述的分组之后,我如何才能转换为仅包含 团、第 1、第 2 列的数据框?
我认为你试图从数据框中删除那家公司,所以使用
df.columns.name = ''
如果您想重置索引,请使用
df = df.reset_index()
输出:
ndf = df['preTestScore'].groupby([df['regiment'], df['company']]).mean().unstack()
ndf.columns.name = ''
1st 2nd regiment Dragoons 3.5 27.5 Nighthawks 14.0 16.5 Scouts 2.5 2.5
To have dataframe simply has regiment, 1st, 2nd as its columns
ndf = ndf.reset_index()
regiment 1st 2nd 0 Dragoons 3.5 27.5 1 Nighthawks 14.0 16.5 2 Scouts 2.5 2.5
通过使用rename_axis
df.rename_axis(None,1)