The Wayback Machine - https://web.archive.org/web/20240618004442/https://www.geeksforgeeks.org/how-to-remove-index-column-while-saving-csv-in-pandas/
Open In App

How to Remove Index Column While Saving CSV in Pandas

Last Updated : 02 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Screenshot-(19)

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

Screenshot-(20)

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

Screenshot-(21)

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

Screenshot-(22)

This is the output for the above code.



Similar Reads

Saving a Pandas Dataframe as a CSV
In this article, we will learn how we can export a Pandas DataFrame to a CSV file by using the Pandas to_csv() method. By default, the to csv() method exports DataFrame to a CSV file with row index as the first column and comma as the delimiter. Creating DataFrame to Export Pandas DataFrame to CSV C/C++ Code # importing pandas as pd import pandas a
2 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 to change the automatically generated column name
6 min read
How to skip rows while reading csv file using Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. Pandas package is one of them and makes importing and analyzing data so much easier. Here, we will discuss how to skip rows while reading csv file. We will use read_csv() method of Pandas library for this task. Syntax: pd.read_csv(fil
3 min read
Saving Text, JSON, and CSV to a File in Python
Python allows users to handle files (read, write, save and delete files and many more). Because of Python, it is very easy for us to save multiple file formats. Python has in-built functions to save multiple file formats. Opening a text file in Python Opening a file refers to getting the file ready either for reading or for writing. This can be don
5 min read
Scrape IMDB movie rating and details using Python and saving the details of top movies to .csv file
We can scrape the IMDb movie ratings and their details with the help of the BeautifulSoup library of Python. Modules Needed: Below is the list of modules required to scrape from IMDB. requests: Requests library is an integral part of Python for making HTTP requests to a specified URL. Whether it be REST APIs or Web Scraping, requests must be learne
4 min read
Saving scraped items to JSON and CSV file using Scrapy
In this article, we will see how to use crawling with Scrapy, and, Exporting data to JSON and CSV format. We will scrape data from a webpage, using a Scrapy spider, and export the same to two different file formats. Here we will extract from the link http://quotes.toscrape.com/tag/friendship/. This website is provided by the makers of Scrapy, for l
5 min read
How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
Get column index from column name of a given Pandas DataFrame
In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together. Syntax: DataFrame.columns Return: column names index Syntax: Index.get_loc(key, method=None, tolerance=None) Return: loc : int if unique index, slice if monotonic index, els
2 min read
Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
This article demonstrates multiple examples to convert the Numpy arrays into Pandas Dataframe and to specify the index column and column headers for the data frame. Example 1: In this example, the Pandas dataframe will be generated and proper names of index column and column headers are mentioned in the function. This approach can be used when ther
4 min read
Read CSV File without Unnamed Index Column in Python
Whenever the user creates the data frame in Pandas, the Unnamed index column is by default added to the data frame. The article aims to demonstrate how to read CSV File without Unnamed Index Column using index_col=0 while reading CSV. Read CSV File without Unnamed Index Column Using index_col=0 while reading CSVThe way of explicitly specifying whic
2 min read