

How to Drop Index Column of a Pandas DataFrame
source link: https://thispointer.com/how-to-drop-index-column-of-a-pandas-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Drop Index Column of a Pandas DataFrame – thisPointer
In this article, we will discuss about the different ways to drop the Index Column of a Pandas DataFrame.
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s create a dataframe with 4 rows and 4 columns
import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','r/python']}) # Display dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 r/python
Let’s set the index column to the above dataframe. We can do this by using set_index() function. This function will take Index column values as a parameter with the method pandas.Index(). The column values are separated by comma operator.
Syntax is as follows:
df.set_index([pandas.Index(['index_columns'])])
where,
Advertisements

- df is the input dataframe
- index_columns contains the column values to be specified in index column.
Example: Set the index column with values ‘s-1’ to ‘s-4’ for the above dataframe.
# set the index values for the above dataframe with # s-1 to s-4 df = df.set_index([pd.Index(['s-1', 's-2', 's-3', 's-4'])]) # display dataframe print(df)
Output:
id name age subjects s-1 58 sravan 22 java s-2 59 jyothika 21 php s-3 60 preethi 22 sql s-4 61 srinadh 23 r/python
Drop the index column of Pandas DataFrame
We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1. where n is the number of rows in the dataframe.
Syntax is as follows:
df.reset_index(drop=True, inplace=True)
where,
- df is the input dataframe
- drop parameter is set to True to drop the index column, if it is set to false, it will not drop the index column.
- inplace parameter is used to replace the dataframe with modified dataframe when it set to True
Example: Here we are going to drop index column for the above dataframe.
# drop the index columns df.reset_index(drop=True, inplace=True) # display dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 r/python
Here the index columns will get reset
Drop the index column of Pandas DataFrame by Exporting to CSV
Here we are going to export our dataframe to csv file and remove the index column while exporting . We can export using to_csv() method by setting index parameter to False. Syntax is as follows:
df.to_csv('file_name.csv', index=False)
where,
- df is the existing dataframe
- file_name is the name of the file
- index parameter is used to drop the index column which set to False
Example: In this example, we are going to export our dataframe to csv named cav_data.csv
# export the dataframe to csv by # dropping the index column df.to_csv('csv_data.csv', index=False)
Let’s open the csv file to see the output.
id,name,age,subjects 58,sravan,22,java 59,jyothika,21,php 60,preethi,22,sql 61,srinadh,23,r/python
Drop the index column of Pandas DataFrame by importing from csv
Here we are going to import the dataframe from the csv file by removing the index column . Syntax is as follows:
pandas.read_csv('file_name.csv', index_col=False)
where,
- file_name is the name of the file to be imported
- index_col parameter is used to drop the index column which set to False
Example: In this example, we are going to import our csv named cav_data.csv to df
# read the dataframe by dropping the index column df = pd.read_csv('csv_data.csv', index_col=False) # display dataframe print(df)
Let’s see the dataframe
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 r/python
Summary
In this article we discussed several ways to drop index column in pandas DataFrame.
Advertisements
Recommend
-
12
How to drop rows in Pandas DataFrame by index labels? Last Updated: 02-07-2020 Pandas provide data analysts a way to delete and filter data frame using
-
122
Count number of Zeros in Pandas Dataframe Column This article will discuss how to count the number of zeros in a single or all column of a Pandas Dataframe. Let’s first create a Dataframe from a list of...
-
10
Pandas | Count non-zero values in Dataframe Column This article will discuss how to count the number of non-zero values in one or more Dataframe columns in Pandas. Let’s first create a Dataframe from a...
-
15
Pandas – Count True Values in a Dataframe Column In this article, we will discuss different ways to count True values in a Dataframe Column. First of all, we will create a Dataframe from a list of tuples...
-
16
This article will discuss how to rename column names in Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.D...
-
14
Check if a Column exists in Pandas DataFrame In this article, we will discuss how to check if a column or multiple columns exist in a Pandas DataFrame or not. Suppose we have a DataFrame, ...
-
9
In this article, we will discuss different ways to delete a column from a DataFrame in Pandas. Table Of Contents What is Pandas DataFrame? Pandas DataFrame is a labelled two di...
-
8
How to Drop Rows in Pandas DataFrame by Index LabelsSkip to content
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
6
Add Column to Pandas DataFrame with constant value In this article, we will learn different ways to add a column in a DataFrame with a constant value. Table Of Contents Sup...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK