The to_excel() method is used to export the DataFrame to the excel file. To write a single object to the excel file, we have to specify the target file name. If we want to write to multiple sheets, we need to create an ExcelWriter object with target filename and also need to specify the sheet in the file in which we have to write. The multiple sheets can also be written by specifying the unique sheet_name. It is necessary to save the changes for all the data written to the file.
data.to_excel( excel_writer, sheet_name='Sheet1', \*\*kwargs )
Arguments Type Description excel_writer str or ExcelWriter object File path or existing ExcelWriter sheet_name str, default ‘Sheet1’ Name of sheet which will contain DataFrame columns sequence or list of str, optional Columns to write index bool, default True Write row names (index) index_label str or sequence, optional Column label for index column(s) if desired. If not specified, and `header` and `index` are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.
- One can provide the excel file name or the Excelwrite object.
- By default the sheet number is 1, one can change it by input the value of argument “sheet_name”.
- One can provide the name of the columns to store the data by input the value of the argument “columns”.
- By default the index is labeled with numbers as 0,1,2 … and so on, one can change it by passing a sequence of the list for the value of the argument “index”.
Below is the implementation of the above method :
In the above example,
- By default index is labeled as 0,1,…. and so on.
- As our DataFrame has columns names so columns are labeled.
- By default, it is saved in “Sheet1”.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course