Antoine Legrand
Antoine Legrand

Reputation: 5

Multiple Pandas Pivot Table in Excel Sheet

I've written some codes to create 3 different pivot tables. They work perfectly when I want to print them in the Python Console. However when I want to display in the my Excel Sheet, the code only show the last Pivot Table (table 3). I didn t find anything in the Documentation "to excel" or "Pivot Table" about that to print the 3 pivot table in excel at the same time without delete the rest.

xl = pd.ExcelFile(NewTransition_file)
df = xl.parse('DB - Jan - Dec',column=['A:AH'])
pd.read_excel(data)

Table1 = df.loc[df['Repricing Method'] == "Table1"]
Table1 = Table1.pivot_table(index=['Client Company Name'],values=['Original Total Charge','Provider Discount', 'Client Fees','Network Fee'])
Table1.to_excel(data, sheet_name='Table1',startcol=0, startrow=1)


Table2 = df.loc[df['Repricing Method'] == "Table2"]
Table2= Table2.pivot_table(index=['Client Company Name'],values=['Original Total Charge','Provider Discount', 'Client Fees','Network Fee'])
Table2.to_excel(data, sheet_name='Table2', startcol=6, startrow=1)

Table3 = df.loc[df['Repricing Method'].isin(OtherMethod)]
Table3 = other.pivot_table(index=['Client Company Name'], values=['Original Total Charge','Provider Discount', 'Client Fees','Network Fee'])
Table3.to_excel(data, sheet_name='Table3', startcol=11, startrow=1)

Upvotes: 0

Views: 2712

Answers (2)

Tokci
Tokci

Reputation: 1280

Use below , mode is 'a' append and startrow = where you want your pivot table to start in same sheet

with pd.ExcelWriter('some_excel.xlsx', mode='a') as writer:
    pivot_1_df.to_excel(writer,startrow= 0 ,sheet_name='Pivots')
    pivot_2_df.to_excel(writer,startrow= 8 ,sheet_name='Pivots') # starts from 8th  row

make sure pandas is upto date or update using

pip install pandas --upgrade

Upvotes: 0

programmar
programmar

Reputation: 654

Table3 overwrites the others I guess. Use ExcelWriter from pandas instead.

Upvotes: -1

Related Questions