| by Arround The Web | No comments

Pandas Change Index

Pandas” is a popular Python library that can work with different types of indexes, such as numerical, categorical, datetime, or multi-level. “Indexes” are labels that identify the columns and rows of a specified DataFrame. Indexes can also be used for filtering, sorting, grouping, and joining data in Python. The Pandas DataFrame index needs to be changed if we want to use a different column as the index, or if we want to reset the index to the default integer range.

This blog will guide you on changing Pandas DataFrame’s index value via the below content:

How to Change the Index Value of the Pandas DataFrame in Python?

To change the index value of the Pandas DataFrame, the “DataFrame.set_index()” method is used in Python. Take a look at the following syntax to gain a better understanding of this concept:

DataFrame.set_index(keys, *, drop=True, append=False, inplace=False, verify_integrity=False)

As shown in the syntax above:

  • The “keys” parameter specifies the column or columns that we want to use as the new index. It can be a single column name, a list of column names, or an array-like object.
  • The “drop” parameter indicates whether to drop the columns designated as the new index or not. The “True” value means the columns will be dropped.
  • The “append” parameter is the Boolean value that determines whether to append/add the specified index to the present one or not. The “False” signifies that the existing index will be overwritten.
  • The “inplace” parameter is the Boolean value that determines whether to change the input DataFrame or retrieve a new one. The “False” value indicates that a new DataFrame will be returned.
  • The “verify_integrity” parameter determines whether to check for duplicate values in the new index or not. The “False” value indicates that no check will be performed.

Example 1: Changing Index Value by Setting Single Column as New Index

This example changes the index value by setting the “age” column as a new index:

import pandas

df = pandas.DataFrame({'id-no': ['2118', '2122', '2123'],'name': ['Anna', 'Joseph', 'Henry'],'age': [18, 27, 14],'height': [5.9, 5.2, 6.10]})

print('Given DataFrame:\n', df)

df = df.set_index("age")

print('\nDataFrame After Setting Specific Index:\n',df)

In the above code:

  • The “pandas” module is imported, and the “pandas.DataFrame()” function is employed to construct the Pandas DataFrame.
  • The “df.set_index()” method takes the column name as an argument and sets the DataFrame index.

Output

The specified column “age” has been set as an Index of the Pandas DataFrame.

Example 2: Changing Index Value by Setting Multiple Columns as New Index

Use the following code to change the index value and set the multiple columns as a new index:

import pandas

df = pandas.DataFrame({'id-no': ['2118', '2122', '2123'],'name': ['Anna', 'Joseph', 'Henry'],'age': [18, 27, 14],'height': [5.9, 5.2, 6.10]})

print('Given DataFrame:\n', df)

df = df.set_index(["age", "height"])

print('\nDataFrame After Setting Specific Index:\n',df)

Here in this code:

  • Multiple column names are passed to the “df.set_index()” method to change the pre-defined index and set the multiple columns as a new index of DataFrame.
  • The modified DataFrame is displayed on the screen using the “print()” function.

Output

The “age” and “height” columns have been added as a new index of the DataFrame.

Example 3: Adding/Setting Custom Index to Pandas DataFrame

This example adds a custom index to Pandas DataFrame:

import pandas

df = pandas.DataFrame({'id-no': ['2118', '2122', '2123'],'name': ['Anna', 'Joseph', 'Henry'],'age': [18, 27, 14],'height': [5.9, 5.2, 6.10]})

print('Given DataFrame:\n', df)

index_new = pandas.Index(['A', 'B', 'C'])

df = df.set_index(index_new)

print('\nDataFrame After Setting Specific Index:\n',df)

In the above code:

  • The “pandas.Index()” method creates an index by taking the new index values in the form of a list.
  • The “df.set_index()” method takes the new index as an argument and sets the index to Pandas DataFrame.

Output

The original index of the Pandas DataFrame has been changed to the newly specified custom values.

Conclusion

The “DataFrame.set_index()” method is used to set the single or multiple existing columns as the index of the Pandas DataFrame. We can also modify an existing index with custom index values using the “DataFrame.set_index()” method. This Python guide presented an in-depth analysis of the changing index of the Pandas DataFrame using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply