In this article, we will discuss multiple ways to save a pandas DataFrame to CSV without an index.
Table of Contents
Introduction
Python panda’s library provides a function to save the pandas DataFrame as CSV.
pandas.DataFrame.to_csv(path_or_buf, sep, index, ....)
It accepts a few more arguments as well but here we will discuss a few important arguments only i.e.
Arguments:
– path_or_buf : string containing file path including filename and “.csv” at the end
– sep : field delimiter, default is “,” (to save it as CSV)
– index : True will keep the indexes while False will drop them while saving
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd # List of Tuples employees = [('Shubham', 'India', 'Tech India', 5), ('Riti', 'India', 'India' , 7), ('Shanky', 'India', 'PMO' , 2), ('Shreya', 'India', 'Design' , 2), ('Aadi', 'US', 'Tech', 11), ('Sim', 'US', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience']) print(df)
Contents of the created dataframe are,
Name Location Team Experience 0 Shubham India Tech India 5 1 Riti India India 7 2 Shanky India PMO 2 3 Shreya India Design 2 4 Aadi US Tech 11 5 Sim US Tech 4
Let’s discuss different ways to save a pandas DataFrame to CSV without an index.
Method 1: Using the “index” attribute
The simplest way to store the pandas DataFrame as a CSV file without an index is to set the “index” attribute as False in the to_csv function. Let’s save the above df as a CSV without index.
# save the DataFrame without index df.to_csv("file.csv", index=False)
The output of the file looks like as below.
Contents of the file file.csv will be,
Name,Location,Team,Experience Shubham,India,Tech India,5 Riti,India,India,7 Shanky,India,PMO,2 Shreya,India,Design,2 Aadi,US,Tech,11 Sim,US,Tech,4
It will save the pandas dataframe to file.csv without index. Please check the contents of file.csv file. There should be no index present in the CSV file saved.
Method 2: Replace the index with any column while saving csv file
Another alternate method is to first replace the index with the first column and then save the pandas DataFrame as CSV. Let’s set the index as the values from the “Name” column here and then save it using the to_csv function.
# set index as Name column df.set_index("Name", inplace=True) # save the DataFrame (here we don't need to set index attribute) df.to_csv("file.csv")
Contents of the file file.csv will be,
Name,Location,Team,Experience Shubham,India,Tech India,5 Riti,India,India,7 Shanky,India,PMO,2 Shreya,India,Design,2 Aadi,US,Tech,11 Sim,US,Tech,4
Here you go, the output again doesn’t contain any index column. Note that, when we will read this file again, the Name column will be available as a column only and not an index.
It will save the pandas dataframe to file.csv without index. Please check the contents of file.csv file. There should be no index present in the CSV file saved.
The complete example is as follows,
import pandas as pd # List of Tuples employees = [('Shubham', 'India', 'Tech India', 5), ('Riti', 'India', 'India' , 7), ('Shanky', 'India', 'PMO' , 2), ('Shreya', 'India', 'Design' , 2), ('Aadi', 'US', 'Tech', 11), ('Sim', 'US', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience']) print(df) # save the DataFrame without index df.to_csv("file.csv", index=False) # set index as Name column df.set_index("Name", inplace=True) # save the DataFrame (here we don't need to set index attribute) df.to_csv("file.csv")
Summary
In this article, we have discussed multiple ways to save the pandas DataFrame as CSV without an index.