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 |
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 |
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
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 |
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) |
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.
- Write pandas DataFrame to CSV File
- Write pandas DataFrame to CSV File without Index
- Add Multiple Columns to pandas DataFrame in Python
- Combine Two Text Columns of pandas DataFrame in Python
- Get Column Names of pandas DataFrame as List in Python
- Get Max & Min Value of Column & Index in pandas DataFrame in Python
- pandas Library Tutorial in Python
- The Python Programming Language
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.