How to Drop the Index Column in Pandas?
In this article, we will discuss how to drop the index column in pandas using Python.
First we have to create the dataframe with student details and set the index by using set_index() function
Syntax:
dataframe.set_index([pandas.Index([index_values…….])])
where
- dataframe is the input dataframe
- Index_values are the values to be given as indexes to the dataframe
Example: Setting index column for the dataset. The initial plot does that the changes are apparent.
Python3
import pandas as pd
data = pd.DataFrame({
"id" : [ 7058 , 7059 , 7072 , 7054 ],
"name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ],
"subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ]
}
)
data = data.set_index(
[pd.Index([ 'student-1' , 'student-2' , 'student-3' , 'student-4' ])])
print (data)
|
Output:
Now we can drop the index columns by using reset_index() method. It will remove the index values and set the default values from 0 to n values
Syntax:
dataframe.reset_index(drop=True, inplace=True)
where
- dataframe is the input dataframe
- drop is set to True to remove index values
- inplace is to set the default integers
Example: Drop the index columns
Python3
import pandas as pd
data = pd.DataFrame({
"id" : [ 7058 , 7059 , 7072 , 7054 ],
"name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ],
"subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ]
}
)
data = data.set_index(
[pd.Index([ 'student-1' , 'student-2' , 'student-3' , 'student-4' ])])
print (data)
data.reset_index(drop = True , inplace = True )
print (data)
|
Output:
Last Updated :
28 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...