Python Pandas DataFrame: reindex() function

reindex() function is used to change the index of rows and columns of a DataFrame. It changes the row label and column label to a dataframe.

Reindexing is used to conform DataFrame to new index with optional filling logic and to place NaN in that location where the values are not present in the previous index. It produce a new object unless the new indexis equivalent to the current one, and values of copy becomes False.

We can reindex the single or multiple rows by using the reindex() method.

Syntax

DataFrame.reindex(labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)

Parameters:

  • labels: It refers to the new labels or the index to conform to the axis that is specified by the 'axis'.
  • index , columns : It is optional. It is prefers an index object for avoiding the duplicate data.
  • axis: It is used to give number or axis name which is to be targeted.
  • method: It is used to fill the holes in  reindexed DataFrame. 
  • copy: It returns a new object, even if the passed indexes are the same.
  • level: It broadcast across a level, matching Index values on the passed MultiIndex level.
  • fill_value: It is the values which are used in place of missing values.
  • limit: The maximum number of consecutive elements that are to be forward or backward fill.
  • tolerance:The maximum distance between original and new labels for inexact matches.

Return:

It return series or dataframe with changed index.

Example

import pandas as pd
df=pd.DataFrame({'Name':['Alice','John','Joey'],'Age':[19,15,14],
                 'Height':[145.7,152.6,148.7]})

index = [1,2,3]
df.index=index 

print(df)

Output

    Name  Age  Height
1  Alice   19   145.7
2   John   15   152.6
3   Joey   14   148.7
new_index=[20,2,3]
print(df.reindex(new_index))

Output

    Name   Age  Height
20   NaN   NaN     NaN
2   John  15.0   152.6
3   Joey  14.0   148.7

We can fill the missing values by using fill_value

df.reindex(new_index, fill_value=0)

Output

   Name   Age  Height
20   0    0     0
2   John  15.0   152.6
3   Joey  14.0   148.7
Tue, 11/16/2021 - 15:02

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.