Pandas

Pandas to_excel : Step By Step Implementation with Example

Pandas to_excel is actually dataframe.to_excel() function. This function helps to export a pandas dataframe into excel file. We can parameterize columns, index, sheet_name, excel_writer object etc. In this article, We will see the implementation

pandas to_excel : Syntax and Implementation-

Well, Inorder to understand dataframe.to_excel() function, We need to create a dummy dataframe. Then we apply the function over it and export the file. We will do in steps. Lets start.

Step 1: Dataframe creation

It is a prerequesties step. Here we will create a simple pandas dataframe.

data = {"Date":["12/4/2020","13/1/2020","14/4/2020","1/11/2019"],
"Open":[11,23,33,41],"Close":[51,63,72,81],"Volume":[1330,2030,8876,4100]}
df = pd.DataFrame(data=data)

Step 2: Appling dataframe.to_excel() –

This is the most important step. It will directly export the dataframe into excel file.

df.to_excel("exported.xlsx")
pandas to_excel implementation

 

Other Parameters in Pandas to_excel : tweak

We can tweak this export by using these below parameter.

1.columns –

By default, I will cover all in the columns in excel file. But using this parameter, We can select only those which we need in the respective file.

2. index-

By default index will start from 0 and will go like 1,2,3 as per the number of the rows. But in the place of that we can use any other sequence like list. Only we need to provide the list or sequence as an parameter value to the index.

3. sheet_name –

Sheet_name is default as sheet1. But we can parameterize with other name in str format.

4. excel_writer object –

It should be the name of the file with full path. If we just pass the file name, It will create the file at the same location.But if we provide the customize path. It will export the excel file at the custom path.

 

I hope you you may easily export the excel files from pandas dataFrame. Also the above parameter, Gives the next level customization. Please comment if you like this article. You may also suggest some improvement area as well.

 

Thanks 

Data Science Learner Team