Signup/Sign In
PUBLISHED ON: MARCH 30, 2021

Pandas DataFrame reindex() Method

In this tutorial, we will discuss and learn the Python pandas DataFrame.reindex() method. By using this method, we can change the name of the index and columns. This method reconciles the DataFrame to the new index with optional filling logic. It places null values in locations having no value in the previous index.

The below is the syntax of the DataFrame.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)

Example 1: Reindex the DataFrame in Pandas

Here, we reindex the index of the DataFrame using the DataFrame.reindex() method. The index which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame([[1,6,2],[3,4,6],[12,1,0]],columns=['A','B','C'],index=(['index_1','index_2','index_3']))
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
index=['index_1','index_2','index_4']
print(df.reindex(index))


--------The DataFrame is----------
A B C
index_1 1 6 2
index_2 3 4 6
index_3 12 1 0
---------------------------------
A B C
index_1 1.0 6.0 2.0
index_2 3.0 4.0 6.0
index_4 NaN NaN NaN

Example 2: Reindex the DataFrame in Pandas

We can also reindex the column of the DataFrame using the DataFrame.reindex() method. The column which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame([[1,6,2],[3,4,6],[12,1,0]],columns=['A','B','C'],index=(['index_1','index_2','index_3']))
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
column=['A','C','D']
print(df.reindex(column,axis="columns"))


--------The DataFrame is----------
A B C
index_1 1 6 2
index_2 3 4 6
index_3 12 1 0
---------------------------------
A C D
index_1 1 2 NaN
index_2 3 6 NaN
index_3 12 0 NaN

Example 3: Reindex the DataFrame in Pandas

We can fill the null values using the parameter fill_value=2 in the DataFrame.reindex() method. After changing the index name, if there is a null value, that null values will be filled by the value 2.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df= pd.DataFrame([[1,6,2],[3,4,6],[12,1,0]],columns=['A','B','C'],index=(['index_1','index_2','index_3']))
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
index=['index_1','index_2','index_4']
print(df.reindex(index,fill_value=2))


--------The DataFrame is----------
A B C
index_1 1 6 2
index_2 3 4 6
index_3 12 1 0
---------------------------------
A B C
index_1 1 6 2
index_2 3 4 6
index_4 2 2 2

Conclusion

In this tutorial, we learned the Python pandas DataFrame.reindex() method. We learned syntax, parameters, and solved examples by applying this method on the DataFrame.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.