Save pandas DataFrame as CSV File in Python (2 Examples)

In this tutorial you’ll learn how to save a pandas DataFrame as a CSV file in the Python programming language.

Setting up the Examples

import pandas as pd                       # Load pandas library
my_df = pd.DataFrame({'A':range(0, 5),    # Construct pandas DataFrame in Python
                      'B':['x', 'y', 'x', 'x', 'y'],
                      'C':range(15, 10, - 1)})
print(my_df)
#    A  B   C
# 0  0  x  15
# 1  1  y  14
# 2  2  x  13
# 3  3  x  12
# 4  4  y  11

Example 1: Exporting pandas DataFrame as CSV File with Default Settings

data.to_csv('my_df_a.csv')                # Saving pandas DataFrame as CSV

Example 2: Ignore Index when Exporting pandas DataFrame as CSV File

data.to_csv('my_df_b.csv',                # Saving pandas DataFrame as CSV
            index = False)

Further Resources & Related Tutorials

Furthermore, you may want to read the related tutorials on my homepage.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top