Write pandas DataFrame to CSV File without Index in Python (Example)
In this Python tutorial you’ll learn how to export a pandas DataFrame to a CSV file without index.
Table of contents:
So now the part you have been waiting for – the exemplifying Python code!
Example Data & Libraries
First, we have to import the pandas library:
import pandas as pd # Load pandas library |
import pandas as pd # Load pandas library
As a next step, we’ll also have to create some example data:
data = pd.DataFrame({'x1':range(10, 16), # Create pandas DataFrame 'x2':[3, 9, 2, 3, 7, 8], 'x3':['a', 'b', 'c', 'd', 'e', 'f'], 'x4':range(16, 10, - 1)}) print(data) # Print pandas DataFrame |
data = pd.DataFrame({'x1':range(10, 16), # Create pandas DataFrame 'x2':[3, 9, 2, 3, 7, 8], 'x3':['a', 'b', 'c', 'd', 'e', 'f'], 'x4':range(16, 10, - 1)}) print(data) # Print pandas DataFrame
Table 1 illustrates that our example pandas DataFrame consists of six rows and four columns.
Let’s print these data to a CSV file!
Example: Write pandas DataFrame as CSV File without Index
In this example, I’ll demonstrate how to save a pandas DataFrame to a CSV file without showing the index numbers of this data set in the final output.
For this task, we can apply the to_csv function as shown below.
In the first line of the following code, we have to specify the working directory and the file name. Note that you would have to replace the working directory path to your own working directory.
The second line of the following syntax is responsible for ignoring the indices. As you can see, we are setting the index argument to be equal to the logical indicator False.
data.to_csv('C:/Users/Joach/Desktop/my directory/data.csv', # Specify path & file name index = False) # Export to CSV without indices |
data.to_csv('C:/Users/Joach/Desktop/my directory/data.csv', # Specify path & file name index = False) # Export to CSV without indices
After executing the previous Python code, a new CSV file called data without index values will appear in your working directory.
Video & Further Resources
I have recently published a video on my YouTube channel, which illustrates the examples of this article. You can find the video below:
The YouTube video will be added soon.
Furthermore, you could read some of the other tutorials on this website. You can find some posts below.
- pandas Library Tutorial in Python
- Write pandas DataFrame to CSV File in Python
- Select Rows of pandas DataFrame by Index in Python
- Sort Index of pandas DataFrame in Python
- Get Max & Min Value of Column & Index in pandas DataFrame in Python
- Set Index of pandas DataFrame in Python
- Access Index of Last Element in pandas DataFrame in Python
- Rename Column of pandas DataFrame by Index in Python
- Python Programming Language
This article has shown how to save a pandas DataFrame to a CSV file without index numbers in Python. If you have further questions, let me know in the comments section.