Python Pandas

Pandas DataFrame Remove Index

Pandas provides a powerful and flexible data structure called “DataFrame”, which can store tabular data with rows and columns. A DataFrame has an index that is used to identify and access the rows of the data. The index can be a single column or a multi-level hierarchy of columns. In some cases, we may want to remove the index column of a DataFrame, either because we don’t need it or because we want to use a different column.

This Python post presented a detailed guide on removing a single or multi-index of the Pandas DataFrame using suitable examples.

How to Remove/Delete the Index of Pandas DataFrame in Python?

To remove the Pandas DataFrame index, the “pd.reset_index()” method is employed in Python. It is used to remove/drop the single or multiple indexes of Pandas DataFrame.

Syntax

DataFrame.reset_index(level=None, *, drop=False, inplace=False, col_level=0, col_fill='', allow_duplicates=_NoDefault.no_default, names=None)

In the above syntax:

  • The “level” parameter specifies which levels of the index to remove. By default, it removes all levels.
  • The “drop” parameter indicates whether to drop or insert the old index values in the DataFrame.
  • The “inplace” parameter indicates whether to modify/change the input DataFrame or retrieve a new one.
  • The “col_level” and “col_fill” parameters are used to specify the level of the multi-level columns to insert old index values (default 0) and the name of the other levels of the multi-level columns (default empty string).
  • The “allow_duplicates” parameter indicates whether to allow duplicate column labels to be created. By default, it is False.
  • The “names” parameter is a string or list that specifies the name or names for the columns or columns containing the old index values.

Example 1: Remove/Delete the Index of Pandas DataFrame

To remove the index of DataFrame in Python, utilize the following code:

import pandas
data_frame1 = pandas.DataFrame({'name': ['Anna', 'Joseph', 'Henry'], 'skills': ['Python', 'Java', 'Linux'], 'salary':[4500, 1000, 4500]})
data_frame1 = data_frame1.set_index('name')
print('Given DataFrame:\n', data_frame1)
data_frame1 = data_frame1.reset_index(drop=True)
print('\nAfter Removing Index:\n',data_frame1)

In the above code:

  • The “pandas” library is imported.
  • The “DataFrame()” function creates the Pandas DataFrame with various columns.
  • The “set_index()” method sets the index by taking the specified column name as an argument.
  • The “reset_index()” takes the parameter “drop=True” and removes/drops the index of the Pandas DataFrame.

Output

The DataFrame has been removed successfully.

Example 2: Remove the Multi-Index of Pandas DataFrame

This example is used to remove the multi-index of Pandas DataFrame:

import pandas
data_frame1 = pandas.DataFrame({'name': ['Anna', 'Joseph', 'Henry'], 'id_no': [11, 12, 13],'skills': ['Python', 'Java', 'Linux'], 'salary':[4500, 1000, 4500]})
data_frame1 = data_frame1.set_index(['name', 'id_no'])
print('Given DataFrame:\n', data_frame1)
data_frame1 = data_frame1.reset_index(drop=True)
print('\nAfter Removing Index:\n',data_frame1)

In the above code block,

  • The “set_index()” method takes the multiple column names as an argument and sets the multi-index.
  • The “reset_index()” method takes the “drop=True” as an argument and removes both the index of Pandas DataFrame.

Note: To drop a single index from multi-index Pandas DataFrame, the “level=” parameter is used in the “df.reset_index()” method.

Output

The multi-index has been removed from the Pandas DataFrame.

Conclusion

The “pd.reset_index()” method of the “pandas” module is used to remove/drop the single or numerous indexes of the specified DataFrame. The “pd.reset_index()” takes the “drop=True” parameter and removes the specified index from the Pandas DataFrame. It can also be utilized to remove/delete the multi-index of Pandas DataFrame. This Python guide presented a complete overview of removing the Pandas DataFrame index using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.