Write pandas DataFrame to CSV File with & without Header in Python (2 Examples)


This tutorial demonstrates how to save a pandas DataFrame as a CSV File with and without header in Python programming.

The article contains this content:

Let’s start right away:


Example Data & Software Libraries

We first need to load the pandas library:

import pandas as pd                        # Import pandas

Furthermore, have a look at the example data below:

data = pd.DataFrame({'x1':range(9, 16),    # Create pandas DataFrame
                     'x2':[2, 3, 6, 2, 3, 1, 8],
                     'x3':['a', 'b', 'c', 'd', 'e', 'f', 'g'],
                     'x4':range(27, 20, - 1)})
print(data)                                # Print pandas DataFrame


table 1 DataFrame write pandas dataframe csv file without header python


Have a look at the previous table. It shows that the pandas DataFrame is constructed of seven rows and four columns.


Example 1: Write pandas DataFrame as CSV File with Header

In Example 1, I’ll show how to create a CSV file containing a pandas DataFrame with a header.

This task is actually quite straightforward, since Python exports the header of a data set by default.

If we want to write a pandas DataFrame to a CSV file with a header, we can use the to_csv function as shown below:

data.to_csv('data_header.csv')             # Export pandas DataFrame as CSV

After running the previous Python code, a new CSV file containing one line with the column names of our pandas DataFrame will appear in your working directory.


Example 2: Write pandas DataFrame as CSV File without Header

Example 2 shows how to create a CSV output containing a pandas DataFrame where the header is ignored.

For this, we have to specify the header argument within the to_csv function as shown in the following Python syntax:

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

After executing the Python code above, another CSV file will show up, which has no header in the first row of the file.


Video & Further Resources

Do you want to know more about the printing of a pandas DataFrame to a CSV File with and without header? Then I recommend watching the following video on my YouTube channel. In the video, I demonstrate the Python programming code of this tutorial.


The YouTube video will be added soon.


Besides that, you may read some of the other tutorials on my website. I have published several tutorials about topics such as text elements, indices, and lists.


Summary: In this post, I have illustrated how to export, download, print, and save a pandas DataFrame as a CSV File with and without header in the Python programming language. Let me know in the comments, if you have additional comments or questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.