How to Remove Index Column While Saving CSV in Pandas
Last Updated :
28 Apr, 2025
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
This is the output for the above code.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
This is the screenshot for the above code.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
This is the screenshot for the above code.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
This is the output for the above code.
Similar Reads
How to Drop Index Column in Pandas? When working with Pandas DataFrames, it's common to reset or remove custom indexing, especially after filtering or modifying rows. Dropping the index is useful when:We no longer need a custom index.We want to restore default integer indexing (0, 1, 2, ...).We're preparing data for exports or transfo
2 min read
Set the First Column and Row as Index in Pandas In Pandas, an index is a label that uniquely identifies each row or column in a DataFrame. Let's learn how to set the first column and row as index in Pandas DataFrame. Set First Column as Index in PandasConsider a Pandas DataFrame, to set the "Name" column as the index, use the set_index method: Py
3 min read
How to Convert Index to Column in Pandas Dataframe? Pandas is a powerful tool which is used for data analysis and is built on top of the python library. The Pandas library enables users to create and manipulate dataframes (Tables of data) and time series effectively and efficiently. These dataframes can be used for training and testing machine learni
2 min read
How to remove numbers from string in Python - Pandas? In this article, let's see how to remove numbers from string in Pandas. Currently, we will be using only the .csv file for demonstration purposes, but the process is the same for other types of files. The function read_csv() is used to read CSV files. Syntax: Â for the method 'replace()': str.replace
2 min read
Rename column by index in Pandas A column of a data frame can be changed using the position it is in known as its index. Just by the use of the index, a column can be renamed. Dealing with large and complex datasets in Pandas often requires manipulating column names for better analysis. Renaming columns by their index position can
6 min read
Rename column name with an index number of the CSV file in Pandas In this blog post, we will learn how to rename the column name with an index number of the CSV file in Pandas.Renaming Column Name with an Index NumberPandas, an advanced data manipulation package in Python, includes several methods for working with structured data such as CSV files. You might wish
6 min read