Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas to_csv() Method



Theto_csv() method in Python's Pandas library provides a convenient way to write data stored in a PandasDataFrame orSeries object to a CSV file. This is particularly useful for data storage, sharing, and further analysis in other applications. This method can handle compression, support various storage options, and allow customization using numerous parameters.

CSV (Comma-Separated Values) is a common plain-text format for storing tabular data, where rows are represented as lines and columns are separated by commas ",". It is a common delimiter for CSV files, and the file has.csv extension.

Syntax

Below is the syntax of the Python Pandas to_csv() method −

DataFrame.to_csv(path_or_buf=None, *, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', lineterminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.', errors='strict', storage_options=None)

When using theto_csv() method on a Series object, you should call it asSeries.to_csv().

Parameters

The Python Pandas to_csv() method accepts the below parameters −

  • path_or_buf: This parameter accepts a string, path object, or file-like object, representing the location of the CSV file. If not specified, the result is returned as a string.

  • sep: Specifies the delimiter(Character or regex pattern) to use. Defaults to a comma (,).

  • na_rep: String to represent missing values.

  • float_format: This parameter determines format string for floating-point numbers.

  • columns: A list of column labels to include in the output. If not specified, all columns are written.

  • header: Boolean or list of strings. IfTrue, includes column labels. Otherwise, no headers are written.

  • index: Whether to include the DataFrame's index in the output CSV file. By default, writes the DataFrame index.

  • index_label: Specifies a label for the index columns.

  • mode: Determines file opening mode. 'w' truncate the file first, 'a' for append, and 'x' for exclusive creation, failing if the file already exists.

  • encoding: A string representing the encoding of the file (e.g., 'utf-8').

  • compression: Specifies the compression method to use. If set to 'infer', the method will automatically detect the compression type based on the file extension (e.g., .gz, .bz2, .zip). You can also pass a dictionary to customize compression methods such as gzip, zip, bz2, zstd, etc. If set to None, no compression will be applied.

  • chunksize: Writes rows in chunks of this size.

  • storage_options: Additional options for connecting to certain storage back-ends (e.g., AWS S3, Google Cloud Storage).

  • errors: It specifies how to handle encoding and decoding errors.

  • Additional parameters: It takes many other parameters for fine tuning data storage operations.

Return Value

The Pandasto_csv() method returns a string ifpath_or_buf is not specified. Otherwise, it saves the data to the specified location.

Example: Creating CSV file from Pandas Series

Here is a basic example demonstrating creating CSV file from Pandas Series using the PandasSeries.to_csv() method.

import pandas as pd# Create a Pandas Seriess = pd.Series([1, 2, 3], index=["duck", "cat", "wolf"])# Save the Series in a CSV files.to_csv('series_data.csv')print("Series is successfully saved in a CSV file..")

Following is an output of the above code −

Series is successfully saved in a CSV file..
If you visit the folder where the CSV files are saved, you can observe the generated CSV file.

Example: Exporting a DataFrame to a CSV File

This example exports the data in a Pandas DataFrame object to CSV file using the Pandasto_csv() method.

import pandas as pd# Create an CSV file enginedf = pd.DataFrame({'name': ['Ravi', 'Priya', 'Kiran', ''], 'salary': [50000, 45000, 65000, 55000]})# Export the DataFrame to a CSV filedf.to_csv('DataFrame_data.csv')print("DataFrame has been saved to 'DataFrame_data.csv'.")

When we run above program, it produces following result −

DataFrame has been saved to 'DataFrame_data.csv'.

Example: Saving DataFrame to a CSV file with index

This example demonstrates saving the DataFrame to a CSV file and include a custom label for the index, using theindex_label parameter of theDataFrame.to_csv() method.

import pandas as pd# Create a sample DataFrame with car datadata = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],'Date_of_purchase': ['2025-01-01', '2025-01-02', '2025-01-07',                          '2025-01-06', '2025-01-09', '2025-01-22']}# Create the DataFramedf = pd.DataFrame(data)# Save the DataFrame to a CSV file, including a custom index labeldf.to_csv('car_with_index.csv', index_label="ID")print("DataFrame has been saved with index as 'car_with_index.csv'.")# Read the saved CSV file back into a DataFrameresult = pd.read_csv('car_with_index.csv')print("\nDataFrame loaded from CSV file:")print(result)

While executing the above code we obtain the following output −

DataFrame has been saved with index as 'car_with_index.csv'.DataFrame loaded from CSV file:
IDCarDate_of_purchase
00BMW2025-01-01
11Lexus2025-01-02
22Audi2025-01-07
33Mercedes2025-01-06
44Jaguar2025-01-09
55Bentley2025-01-22

Example: Saving DataFrame to a CSV file with Missing Data

The following example demonstrates using theto_csv() method for handling the missing values. Here missing values in the CSV file are represented as "--" string.

import pandas as pd# Create a sample DataFrame with car datadata = {    'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', '', 'Bentley'],    'Color': ['red', 'yellow', None, 'black', 'white', 'blue'],    'Date_of_purchase': [None, '2025-01-02', '2025-01-07',                          '2025-01-06', '2025-01-09', '2025-01-22']}# Create the DataFramedf = pd.DataFrame(data)# Save the DataFrame to a CSV filedf.to_csv("missing_data.csv", index=False, na_rep="--")print("DataFrame with missing values saved as 'missing_data.csv'.")# Read the saved CSV file back into a DataFrameresult = pd.read_csv('missing_data.csv')print("\nDataFrame loaded from CSV file:")print(result)

Following is an output of the above code −

DataFrame with missing values saved as 'missing_data.csv'.DataFrame loaded from CSV file:
CarColorDate_of_purchase
0BMWred--
1Lexusyellow2025-01-02
2Audi--2025-01-07
3Mercedesblack2025-01-06
4NaNwhite2025-01-09
5Bentleyblue2025-01-22

Example: Appending Data to an Existing CSV File

This example demonstrates appending data to an existing CSV file using themode='a' parameter of theDataFrame.to_csv() method.

import pandas as pd# Create a sample DataFrame with car datadata = {    'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', '', 'Bentley'],    'Color': ['red', 'yellow', None, 'black', 'white', 'blue'],    'Date_of_purchase': [None, '2025-01-02', '2025-01-07',                          '2025-01-06', '2025-01-09', '2025-01-22']}# Create the DataFramedf = pd.DataFrame(data)# Save the DataFrame to a CSV filedf.to_csv("missing_data.csv", index=False, na_rep="--")# Create a new DataFramenew_data = {    "Car": ["Ferrari", "Honda"],    "Color": ['red', 'black'],    "Date_of_purchase": ['2024-10-23', "2025-01-01"]}df_new = pd.DataFrame(new_data)# Append data to an existing CSV filedf_new.to_csv("missing_data.csv", mode="a", index=False, header=False)print("New data has been appended to 'missing_data.csv'.")# Read the saved CSV file back into a DataFrameresult = pd.read_csv('missing_data.csv')print("\nDataFrame loaded from CSV file:")print(result)

While executing the above code we obtain the following output −

New data has been appended to 'missing_data.csv'.DataFrame loaded from CSV file:
CarColorDate_of_purchase
0BMWred--
1Lexusyellow2025-01-02
2Audi--2025-01-07
3Mercedesblack2025-01-06
4NaNwhite2025-01-09
5Bentleyblue2025-01-22
6Ferrarired2024-10-23
7Hondablack2025-01-01
python_pandas_io_tool.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp