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
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.
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)
This is the most important step. It will directly export the dataframe into excel file.
df.to_excel("exported.xlsx")
We can tweak this export by using these below parameter.
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.
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.
Sheet_name is default as sheet1. But we can parameterize with other name in str format.
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