Python - 计算数据透视表中总计的百分比
Python - Calculating Percent of Grand Total in Pivot Tables
我有一个数据框,我使用 pd.pivot_table 方法和求和聚合函数将其转换为数据透视表 table:
summary = pd.pivot_table(df,
index=["Region"],
columns=["Product"],
values=['Price'],
aggfunc=[np.sum],
fill_value=0,
margins=True,
margins_name="Total"
)
我收到了这样的输出:
我想添加另一个数据透视表 table,显示每个类别在前一个数据透视表 table 中计算的总计百分比。所有这些加起来应该是 100%,应该看起来像这样。
我尝试了在 Whosebug 上找到的以下解决方法:
total = df['Price'].sum()
table = pd.pivot_table(DF,
index=["Region"],
columns=["Product"],
values=['Price'],
aggfunc=[np.sum,
(lambda x: sum(x)/total*100)
],
fill_value=0,
margins=True,
margins_name="Total"
)
这计算了百分比,但它们加起来只有 85%...
最好不要在数据透视表之外计算总数,而只需要从第一个数据透视表调用总计。但是即使我要像上面的代码那样单独计算,只要加起来达到100%就很好了。
提前致谢!
这很容易做到:
import numpy as np
import pandas as pd
# Create table
table_1 = np.matrix([[100, 200, 650, 950],
[200, 250, 350, 800],
[400, 500, 200, 200],
[700, 950, 1200, 2850]])
column_labels = ['A', 'B', 'C', 'Region Total']
idx_labels = ['Region 1', 'Region 2', 'Region 3', 'Product Total']
df = pd.DataFrame(table_1)
df.columns = column_labels
df.index = idx_labels
df.index.name = 'Sales'
# Create percentage table
df_percentage = np.round(df*100/df.iloc[-1, -1], 1)
print(df_percentage)
A B C Region Total
Sales
Region 1 3.5 7.0 22.8 33.3
Region 2 7.0 8.8 12.3 28.1
Region 3 14.0 17.5 7.0 7.0
Product Total 24.6 33.3 42.1 100.0
我有一个数据框,我使用 pd.pivot_table 方法和求和聚合函数将其转换为数据透视表 table:
summary = pd.pivot_table(df,
index=["Region"],
columns=["Product"],
values=['Price'],
aggfunc=[np.sum],
fill_value=0,
margins=True,
margins_name="Total"
)
我收到了这样的输出:
我想添加另一个数据透视表 table,显示每个类别在前一个数据透视表 table 中计算的总计百分比。所有这些加起来应该是 100%,应该看起来像这样。
我尝试了在 Whosebug 上找到的以下解决方法:
total = df['Price'].sum()
table = pd.pivot_table(DF,
index=["Region"],
columns=["Product"],
values=['Price'],
aggfunc=[np.sum,
(lambda x: sum(x)/total*100)
],
fill_value=0,
margins=True,
margins_name="Total"
)
这计算了百分比,但它们加起来只有 85%...
最好不要在数据透视表之外计算总数,而只需要从第一个数据透视表调用总计。但是即使我要像上面的代码那样单独计算,只要加起来达到100%就很好了。
提前致谢!
这很容易做到:
import numpy as np
import pandas as pd
# Create table
table_1 = np.matrix([[100, 200, 650, 950],
[200, 250, 350, 800],
[400, 500, 200, 200],
[700, 950, 1200, 2850]])
column_labels = ['A', 'B', 'C', 'Region Total']
idx_labels = ['Region 1', 'Region 2', 'Region 3', 'Product Total']
df = pd.DataFrame(table_1)
df.columns = column_labels
df.index = idx_labels
df.index.name = 'Sales'
# Create percentage table
df_percentage = np.round(df*100/df.iloc[-1, -1], 1)
print(df_percentage)
A B C Region Total
Sales
Region 1 3.5 7.0 22.8 33.3
Region 2 7.0 8.8 12.3 28.1
Region 3 14.0 17.5 7.0 7.0
Product Total 24.6 33.3 42.1 100.0