Skip to content

Latest commit

 

History

History
60 lines (41 loc) · 2.1 KB

Pandas_Save_dataframe_to_Excel.md

File metadata and controls

60 lines (41 loc) · 2.1 KB



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:

Input

Import libraries

import pandas as pd

Setup Variables

  • file_path: Excel file path as output if you are using Naas it will start with home/ftp/
  • sheet_name: Name of the sheet that will contain the DataFrame
  • index: 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

Model

Create dataframe

# 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

Output

Save DataFrame to Excel file

# 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}")