Save pandas DataFrame as CSV File with & without Header in Python (2 Examples)

In this Python programming tutorial you’ll learn how to convert a pandas DataFrame to a CSV File with and without header.

Example Data

import pandas as pd                       # Import pandas
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 Header

data.to_csv('my_df1.csv',                 # Saving pandas DataFrame as CSV
            header = True)

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

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

Related Tutorials

Below, you may find some additional resources on topics such as counting and groups.

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