In this article, we’ll discuss how to avoid pandas creating an index in a saved CSV file. Pandas is a library in Python where one can work with data. While working with Pandas, you may need to save a DataFrame to a CSV file. The Pandas library includes an index column in the output CSV file by default. Further in the article, we’ll understand the default behavior and find a solution if we don’t want the index in the output of our saved CSV.
Remove Index Column While Saving CSV in Pandas
Below are the ways by which we can avoid pandas creating an index in a saved CSV in Python:
- Pandas to CSV without index
- Pandas to CSV without index and header
- Pandas to CSV without header
- Pandas to CSV by dropping index
Pandas to CSV Without Index
In this example, we are removing the index column directly. The output will be saved in the output.csv file in which the index will be dropped.
Python3
import pandas as pd data = { 'Names' : [ 'Sanjana' , 'Deepthi' , 'Sankeerthana' ]} age = [ '17' , '22' , '33' ] df = pd.DataFrame(data, index = age) print (df) df.to_csv( 'output.csv' , index = False ) |
Output
Pandas to CSV Without Index and Header
To save a DataFrame in Pandas without both header and index, we just need to set both index and header to false. We’ll also use the to_csv method to save these changes in the saved CSV file. The code for this is as follows:
Python3
import pandas as pd data = { 'Rollno' : [ 101 , 202 , 303 ], 'Age' : [ 16 , 17 , 18 ]} sno = [ 1 , 2 , 3 ] df = pd.DataFrame(data,index = sno) print (df) df.to_csv( 'output1.csv' , index = False , header = False ) |
Output
Pandas to CSV Without Header
To save a DataFrame in Pandas without header, we just need to set the header to false. We’ll also use the to_csv method to save these changes in the saved CSV file. The code for this is as follows:
Python3
import pandas as pd data = { 'Rollno' : [ 1 , 2 , 3 ], 'Age' : [ 15 , 16 , 17 ]} df = pd.DataFrame(data) print (df) df.to_csv( 'output2.csv' , header = False ) |
Output
Pandas to CSV by Dropping the Index
When we want to save a DataFrame to a CSV without the index but with the header, we can just drop the index. Here, we can use a special method called ‘iloc‘ provided by Pandas. The iloc method allows integer-based indexing. By using this, we can select specific rows and columns in a Dataframe. Also, by using this, we can do slicing.
Python3
import pandas as pd data = { 'A' : [ 1 , 2 , 3 ], 'B' : [ 4 , 5 , 6 ]} df = pd.DataFrame(data) print (df) df.to_csv( 'withindex.csv' ) df1 = pd.read_csv( 'withindex.csv' ) df1 = df1.iloc[:, 1 :] df1.to_csv( 'withoutindex.csv' , index = False ) |
In the above code, we used the iloc method as df1.iloc[:,1:]. To understand this better, we’ll understand how slicing is being done here “[:,1:]”. So here, ‘:, ‘ represents that we want to select all the rows, and ‘ 1: ‘ represents that we want to select columns from index 1 leaving behind the index 0, therefore dropping the index. Also, we created two CSV files to check whether the change was implemented or not.
Output