Explore Courses Blog Tutorials Interview Questions
+1 vote
in Data Science by (17.6k points)

I use pandas to write to excel file in the following fashion:

import pandas

writer = pandas.ExcelWriter('Masterfile.xlsx') 

data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])

Masterfile.xlsx already consists of number of different tabs. However, it does not yet contain "Main".

Pandas correctly writes to "Main" sheet, unfortunately it also deletes all other tabs.

1 Answer

0 votes
by (41.4k points)

This will work fine:

import pandas

from openpyxl import load_workbook

book = load_workbook('Masterfile.xlsx')

writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl') = book

writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])

If You want to learn data science with python visit this data science tutorial and data science certification by Intellipaat.

by (110 points)
Thank you for this solution! The line concerning the sheets attribute did the trick over other solutions present elsewhere.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.7k questions

32.8k answers


109k users

Browse Categories