HomeLinuxPandas Change Index

Pandas Change Index


Pandas” is a well-liked Python library that may work with several types of indexes, corresponding to numerical, categorical, datetime, or multi-level. “Indexes” are labels that establish the columns and rows of a specified DataFrame. Indexes may also be used for filtering, sorting, grouping, and becoming a member of knowledge in Python. The Pandas DataFrame index must be modified if we wish to use a special column because the index, or if we wish to reset the index to the default integer vary.

This weblog will information you on altering Pandas DataFrame’s index worth through the beneath content material:

How one can Change the Index Worth of the Pandas DataFrame in Python?

To vary the index worth of the Pandas DataFrame, the “DataFrame.set_index()” methodology is utilized in Python. Check out the next syntax to achieve a greater understanding of this idea:

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

As proven within the syntax above:

  • The “keys” parameter specifies the column or columns that we wish to use as the brand new index. It may be a single column identify, a listing of column names, or an array-like object.
  • The “drop” parameter signifies whether or not to drop the columns designated as the brand new index or not. The “True” worth means the columns can be dropped.
  • The “append” parameter is the Boolean worth that determines whether or not to append/add the required index to the current one or not. The “False” signifies that the prevailing index can be overwritten.
  • The “inplace” parameter is the Boolean worth that determines whether or not to vary the enter DataFrame or retrieve a brand new one. The “False” worth signifies {that a} new DataFrame can be returned.
  • The “verify_integrity” parameter determines whether or not to test for duplicate values within the new index or not. The “False” worth signifies that no test can be carried out.

Instance 1: Altering Index Worth by Setting Single Column as New Index

This instance modifications the index worth by setting the “age” column as a brand new index:

import pandas

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

print(‘Given DataFrame:n, df)

df = df.set_index(“age”)

print(nDataFrame After Setting Particular Index:n,df)

Within the above code:

  • The “pandas” module is imported, and the “pandas.DataFrame()” perform is employed to assemble the Pandas DataFrame.
  • The “df.set_index()” methodology takes the column identify as an argument and units the DataFrame index.

Output

Pandas Change Index

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

Instance 2: Altering Index Worth by Setting A number of Columns as New Index

Use the next code to vary the index worth and set the a number of columns as a brand new index:

import pandas

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

print(‘Given DataFrame:n, df)

df = df.set_index([“age”, “height”])

print(nDataFrame After Setting Particular Index:n,df)

Right here on this code:

  • A number of column names are handed to the “df.set_index()” methodology to vary the pre-defined index and set the a number of columns as a brand new index of DataFrame.
  • The modified DataFrame is displayed on the display screen utilizing the “print()” perform.

Output

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

Instance 3: Including/Setting Customized Index to Pandas DataFrame

This instance provides a customized index to Pandas DataFrame:

import pandas

df = pandas.DataFrame({‘id-no’: [‘2118’, ‘2122’, ‘2123’],‘identify’: [‘Anna’, ‘Joseph’, ‘Henry’],‘age’: [18, 27, 14],‘peak’: [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 Particular Index:n,df)

Within the above code:

  • The “pandas.Index()” methodology creates an index by taking the brand new index values within the type of a listing.
  • The “df.set_index()” methodology takes the brand new index as an argument and units the index to Pandas DataFrame.

Output

The unique index of the Pandas DataFrame has been modified to the newly specified customized values.

Conclusion

The “DataFrame.set_index()” methodology is used to set the one or a number of present columns because the index of the Pandas DataFrame. We are able to additionally modify an present index with customized index values utilizing the “DataFrame.set_index()” methodology. This Python information introduced an in-depth evaluation of the altering index of the Pandas DataFrame utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments