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:
import pandas as pd
file_path
: CSV file path as outputsep
: 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
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
df.to_csv(file_path)
print(f"DataFrame successfully saved: {file_path}")