pandas
  1. pandas-dataframeto-excel

DataFrame.to_excel() - ( Pandas DataFrame Basics )

Heading h2

Syntax

DataFrame.to_excel(excel_writer=None, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None)

Example

import pandas as pd
  
df = pd.DataFrame({'Name': ['John', 'Smith', 'Lucy', 'Emma'], 'Age': [32, 25, 18, 42]})
  
df.to_excel('people.xlsx', sheet_name='Sheet1', index=False)

Output

The above code will create an Excel file called 'people.xlsx' in the current directory, with the DataFrame 'df' exported as the first sheet.

Explanation

DataFrame.to_excel() is a method in the pandas library used to write a DataFrame to an Excel file. It takes in various arguments to customize the output excel file.

In the above example, we create a simple DataFrame with columns 'Name' and 'Age', and write it to an Excel file called 'people.xlsx'. The 'Sheet1' parameter specifies the name of the sheet where the DataFrame will be written, and the index=False parameter prevents the DataFrame from being written with the default pandas index as an extra column.

Use

DataFrame.to_excel() is useful in writing pandas DataFrames to Excel files, which can be used for various data analysis and reporting applications.

Important Points

  • DataFrame.to_excel() is a method in the pandas library used to write a DataFrame to an Excel file
  • It takes in various arguments to customize the output excel file
  • The default sheet name is 'Sheet1'
  • The index parameter can be set to False to prevent the DataFrame index from being written as an extra column
  • The float_format parameter can be used to specify the number format for floating-point numbers in the DataFrame

Summary

In conclusion, DataFrame.to_excel() is a useful method in pandas for writing DataFrame objects to Excel files. It is a versatile tool for data analysis, reporting, and sharing. The method allows for customization of the Excel output file, such as the sheet name, number formats, and index columns.

Published on: