我在获得总计栏时遇到困难。要获得它,我使用合并如下;
Total = count*price column
这是我的代码;
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)
df1 = 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)
df2= df.merge(df1,left_on=["Invoice Cost Centre", "Invoice Category"],right_on=["Invoice Cost Centre", "Invoice Category"],how='left').fillna(0)
print(df2)
df2.to_excel('a.xlsx',sheet_name='Invoice Summary')