Categories
Python Answers

How to write to an Excel spreadsheet with Python?

Spread the love

Sometimes, we want to write to an Excel spreadsheet with Python.

In this article, we’ll look at how to write to an Excel spreadsheet with Python.

How to write to an Excel spreadsheet with Python?

To write to an Excel spreadsheet with Python, we can use Pandas.

For instance, we write

from pandas import DataFrame

l1 = [1, 2, 3, 4]
l2 = [1, 2, 3, 4]
df = DataFrame({"Stimulus Time": l1, "Reaction Time": l2})
df.to_excel("test.xlsx", sheet_name="sheet1", index=False)

to create a data frame with the DataFrame class called with a dict.

Then we call to_excel on the df data frame with the file name to write to, the sheet_name, and index set to False to not write the index column to the sheet.

The dict keys are the headers in sheet1 and the list values are the columns.

Conclusion

To write to an Excel spreadsheet with Python, we can use Pandas.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *