How to save a Pandas dataframe to a CSV in Python [3 Examples]

Do you want to save a dataframe to a CSV file? In this Pandas blog, I will explain how to save a Pandas dataframe to a CSV in Python with some examples. And I will also explain how to read a Pandas dataframe to a CSV in Python.

To save a Pandas DataFrame to a CSV file in Python, use the to_csv function. This function allows for customization, such as selecting specific columns, handling special characters, and optionally excluding the header and index. These features provide flexibility in tailoring the CSV output to specific requirements and data formats.

Save a Pandas dataframe to a CSV in Python

The simplest and most common method to save a Pandas dataframe to a CSV in Python is by using the to_csv function:

df.to_csv('filename.csv', index=False)

Here,

  • index: If False, the row index is not written to the CSV file.
  • header: If False, column names are not written.

For example:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Paris', 'London']}

df = pd.DataFrame(data)
df.to_csv('my_data.csv', index=False)

Output: The index=False argument is optional. It tells Pandas not to write row names (indices). If the DataFrame has a meaningful index, we might want to keep it by setting index=True.

Name,Age,City
Alice,25,New York
Bob,30,Paris
Charlie,35,London

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

How to save a Pandas dataframe to a CSV in Python

For customized output:

1. Pandas dataframe to csv with specific columns

We can specify which columns to include in the CSV with the help of the columns parameter.

Here is the full code to save a Pandas dataframe to a CSV in Python:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Paris', 'London']}

df = pd.DataFrame(data)
df.to_csv('my_data.csv', columns=['Name', 'Age'], index=False)

Output:

Name,Age
Alice,25
Bob,30
Charlie,35

After executing the code in Pycharm, one can see the output in the below screenshot.

pandas to csv in Python

2. Dataframe to CSV with handling special characters

If the data includes commas or other special characters, we can specify a different delimiter.

Here is the full code to save a Pandas dataframe to a CSV in Python:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Paris', 'London']}

df = pd.DataFrame(data)
df.to_csv('my_data.csv', sep=';', index=False)

Output:

Name;Age;City
Alice;25;New York
Bob;30;Paris
Charlie;35;London

Below is a screenshot showcasing the output after executing the code in the Pycharm editor.

pandas dataframe to a csv file in Python

3. Saving a Pandas Dataframe as a CSV without headers and index


Here, the dataframe is saved without a header and index numbers using the header and index parameters as False in the to_csv() function.

Here is the full code to save a Pandas dataframe to a CSV in Python:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Paris', 'London']}

df = pd.DataFrame(data)
df.to_csv('my_data.csv', header=False, index=False)

Output:

Alice,25,New York
Bob,30,Paris
Charlie,35,London

The following screenshot, taken after implementing the code in Pycharm, displays the result.

pandas save dataframe as csv in Python without header

How to read a Pandas dataframe to a CSV in Python

To read a CSV file with a header into a DataFrame in Python. The Pandas read_csv function automatically assumes that the first row of our CSV file contains the headers unless told otherwise.

Here is a basic example of how you can use it:

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Paris', 'London']}

df = pd.DataFrame(data)

# Writing a dataframe to a CSV.
df.to_csv('my_data.csv', header=True, index=False)

# Reading a CSV as a dataframe again
df = pd.read_csv('my_data.csv')
print(df.head())

Output:

      Name  Age      City
0    Alice   25  New York
1      Bob   30     Paris
2  Charlie   35    London

The output of the code executed in Pycharm is illustrated in the screenshot below.

pandas read csv with header in Python

Conclusion

Here, I have explained how to save a Pandas DataFrame to a CSV file in Python using the to_csv function, with examples covering the direct approach, selecting specific columns, handling special characters, and saving without headers and indices.

These examples demonstrate the versatility and adaptability of Pandas in exporting DataFrame data to CSV format, catering to a variety of data storage needs.

You may also like to read: