5

I have several .csv files and I want to write them into one .xlsx file as spreadsheets.

I've loaded these .csv files into Pandas.DataFrame using following code:

df1 = pandas.read_csv('my_file1.csv')
df2 = pandas.read_csv('my_file2.csv')
......
df5 = pandas.read_csv('my_file5.csv')

But I couldn't find any functions in Pandas that can write these DataFrames into one .xlsx file as separated spreadsheets.

Can anyone help me with this?

2
  • Could you add some specifics about how you're trying to write them? On separate sheets? Merged somehow? See DataFrame.to_excel() and if it can help you. Commented Aug 23, 2016 at 10:49
  • @IljaEverilä Hi, there. Yes, I want to write them into separate sheets. Commented Aug 23, 2016 at 10:50

1 Answer 1

7

With recent enough pandas use DataFrame.to_excel() with an existing ExcelWriter object and pass sheet names:

from pandas.io.excel import ExcelWriter
import pandas

csv_files = ['my_file1.csv', 'my_file2.csv', ..., 'my_file5.csv']

with ExcelWriter('my_excel.xlsx') as ew:
    for csv_file in csv_files:
        pandas.read_csv(csv_file).to_excel(ew, sheet_name=csv_file)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.