Menu Close

How to write to an Excel spreadsheet with Python?

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.

Posted in Python, Python Answers