Skip to content

Latest commit

 

History

History
62 lines (43 loc) · 2.3 KB

Pandas_Save_dataframe_to_CSV.md

File metadata and controls

62 lines (43 loc) · 2.3 KB



Template request | Bug report | Generate Data Product

Tags: #pandas #csv #snippet #operation

Author: Florent Ravenel

Description: This notebook show how to save a dataframe to a CSV file.

References:

Input

Import libraries

import pandas as pd

Setup Variables

  • file_path: CSV file path as output
  • sep: Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer.
  • header: Row number(s) to use as the column names, and the start of the data.
  • encoding: Encoding to use for UTF when writing (ex. ‘utf-8’). List of Python standard encodings.
  • index: Write row names (index). Boolean default True
file_path = "output.csv"
sep = ","
header = 0
encoding = "utf-8"
index = False

Model

Create dataframe

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

Display result

df.to_csv(file_path)
print(f"DataFrame successfully saved: {file_path}")