Template request | Bug report | Generate Data Product
Tags: #pandas #excel #snippet #operation #dataframe #save
Author: Benjamin Filly
Description: This notebook show how to save a dataframe to Excel.
References:
import pandas as pd
file_path
: Excel file path as output if you are using Naas it will start withhome/ftp/
sheet_name
: Name of the sheet that will contain the DataFrameindex
: Specifies whether to write row names (index) to the Excel file. Boolean, default is True
file_path = "/home/ftp/output.xlsx"
sheet_name = "Name"
index = False
# DataFrame creation
df = pd.DataFrame(
{
"team": ["A", "A", "A", "B", "B", "B"],
"points": [11, 7, 8, 10, 21, 13],
"assists": [5, 7, 7, 9, 12, 0],
"rebounds": [5, 8, 10, 6, 6, 22],
}
)
df
# Saving the DataFrame in an Excel file
df.to_excel(file_path, sheet_name=sheet_name, index=index)
print(f"DataFrame successfully saved: {file_path}")