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,
- 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:
Frequently Asked:
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.