Write Excel with Pandas


Excel files can be created in Python using the module Pandas. In this article we will show how to create an excel file using Python.

Related course:
Data Analysis in Python with Pandas

Write Excel
We start by importing the module pandas. From the module we import ExcelWriter and ExcelFile.
The next step is to create a data frame. In the data frame we put a list, with the name of the list as the first argument:

df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9],
                   'b':[3,5,6,2,4,6,7,8,7,8,9]})

In this short code snippet, ‘a’ and ‘b’ are columns’. The second is the list of data the these columns will contain.

Then we write data frame to an excel file using:

writer = ExcelWriter('Pandas-Example2.xlsx')
df.to_excel(writer,'Sheet1',index=False)
writer.save()

The complete code:

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np
 
df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9],
                   'b':[3,5,6,2,4,6,7,8,7,8,9]})
 
writer = ExcelWriter('Pandas-Example2.xlsx')
df.to_excel(writer,'Sheet1',index=False)
writer.save()

The output of this code:

write excel pandas

Read Excel with Pandas
This entry was posted in Pandas. Bookmark the permalink.