Python 如何使用透视表中的列*值来获取行的和?
我试图得到以下输出。坚持要得到总数 这是我的密码Python 如何使用透视表中的列*值来获取行的和?,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我试图得到以下输出。坚持要得到总数 这是我的密码 def generate_invoice_summary_info(): file_path = 'output.xlsx'
def generate_invoice_summary_info():
file_path = 'output.xlsx'
df = pd.read_excel(file_path, sheet_name='Invoice Details', usecols="E:F,I,L:M")
df['Price'] = df['Price'].astype(float)
# df['Total'] = df.groupby(["Invoice Cost Centre", "Invoice Category"]).agg({'Price': 'sum'}).reset_index()
df = pd.pivot_table(df, index=["Invoice Cost Centre", "Invoice Category"],columns=['Price','Reporting Frequency','Data Feed'],
aggfunc=len ,fill_value=0,margins=True)
print(df.head())
df.to_excel('a.xlsx',sheet_name='Invoice Summary')
上述代码产生以下输出(90%正确)
我被困在寻找总列。
根据count*price
Total = count*price column
如何在透视表中执行此操作?
我使用了边距属性,但它只提供行和
编辑
打印(df):
您能否以Python(打印?)而不是Excel表格的形式共享数据框的输出?此外,数据/代码应该作为文本而不是图像存在于文章中。@AlexanderCécile我添加了打印(df)输出。我的代码也是文本。您能否以Python(打印?)而不是Excel工作表的形式共享数据框的输出?此外,数据/代码应该作为文本而不是图像存在于文章中。@AlexanderCécile我添加了打印(df)输出。我的代码也是文本。不是图像
Price 10.4 ... 85.0 All
Reporting Frequency M ... M
Data Feed BWH EMAIL ... StarBOS
Invoice Cost Centre Invoice Category ...
D3TM Reseller Non Equity 21 10 ... 0 125
EQUITYEMP Baileys 0 7 ... 0 10
Energy NSW 16 0 ... 0 32
Far North Queensland 3 0 ... 0 6
South East 6 0 ... 0 16
Cooper & Dysart 0 0 ... 0 3
Petro Fuel & Lubricants 8 0 ... 0 20
South East QLD Fuels 0 0 ... 0 19
R1M Retail QLD 60 0 ... 0 867