Pandas 如何获得Panda dataframe数据透视表中各列的值总和?
我能够使用下面的代码创建透视表,如下所示Pandas 如何获得Panda dataframe数据透视表中各列的值总和?,pandas,pivot-table,Pandas,Pivot Table,我能够使用下面的代码创建透视表,如下所示 d = {'ID': [1,2,1,2,3], 'Month':["Apr-20","May-20","June-20","Apr-20","June-20"],'Value1':[12,23,15,45,34], 'Value2':[124,214,1234,1324,234], 'Value3':[124,214,1234,1324,234], 'Value4':[124,214,1234,1324,234]} df = pd.DataFrame(d
d = {'ID': [1,2,1,2,3], 'Month':["Apr-20","May-20","June-20","Apr-20","June-20"],'Value1':[12,23,15,45,34], 'Value2':[124,214,1234,1324,234], 'Value3':[124,214,1234,1324,234], 'Value4':[124,214,1234,1324,234]}
df = pd.DataFrame(d)
df_pvt = pd.pivot_table(df, values=['Value1','Value2', 'Value3', 'Value4'],
index=['ID'],
columns=['Month'],
aggfunc=np.sum,
fill_value=0)
我一直在按照下图中的红色标记计算“总值”和总计行。我需要你的帮助。我期望低于输出
有必要在
sum
之后添加另一个数据帧,其中多索引由以下人员创建:
如果要选择一个新添加的列,请使用元组:
print (df_pvt[('Total','Value1')])
ID
1 27
2 68
3 34
Name: (Total, Value1), dtype: int64
另一个想法是新级别的变更顺序:
df = df_pvt.sum(axis=1, level=0)
df.columns = pd.MultiIndex.from_product([df.columns, ['Total'], ])
df_pvt = df_pvt.join(df)
print (df_pvt)
Value1 Value2 Value3 \
Month Apr-20 June-20 May-20 Apr-20 June-20 May-20 Apr-20 June-20 May-20
ID
1 12 15 0 124 1234 0 124 1234 0
2 45 0 23 1324 0 214 1324 0 214
3 0 34 0 0 234 0 0 234 0
Value4 Value1 Value2 Value3 Value4
Month Apr-20 June-20 May-20 Total Total Total Total
ID
1 124 1234 0 27 1358 1358 1358
2 1324 0 214 68 1538 1538 1538
3 0 234 0 34 234 234 234
#swapped order
print (df_pvt[('Value1','Total')])
也可以创建空字符串级别,但我认为这很混乱(因为有必要使用空字符串),所以首选第一种解决方案:
df = df_pvt.sum(axis=1, level=0)
df.columns = pd.MultiIndex.from_product(['Total Sum of ' + df.columns, ['']])
df_pvt = df_pvt.join(df)
print (df_pvt)
Value1 Value2 Value3 \
Month Apr-20 June-20 May-20 Apr-20 June-20 May-20 Apr-20 June-20 May-20
ID
1 12 15 0 124 1234 0 124 1234 0
2 45 0 23 1324 0 214 1324 0 214
3 0 34 0 0 234 0 0 234 0
Value4 Total Sum of Value1 Total Sum of Value2 \
Month Apr-20 June-20 May-20
ID
1 124 1234 0 27 1358
2 1324 0 214 68 1538
3 0 234 0 34 234
Total Sum of Value3 Total Sum of Value4
Month
ID
1 1358 1358
2 1538 1538
3 234 234
如有必要,请使用空字符串:
print (df_pvt[('Total Sum of Value1','')])
ID
1 27
2 68
3 34
Name: (Total Sum of Value1, ), dtype: int64
如果您需要如图所示的行总计,请添加:df_pvt.loc['grand total']=df_pvt.sum()
print (df_pvt[('Total Sum of Value1','')])
ID
1 27
2 68
3 34
Name: (Total Sum of Value1, ), dtype: int64