Updated April 1, 2023
Introduction to Pandas DataFrame.reindex
The following article provides an outline for Pandas DataFrame.reindex. Every data structure which has labels to it will hold the necessity to rearrange the row values, there will also be a necessity to feed a new index itself into the data object based on the necessity. So from a python pandas perspective all these are indexing and rearrangement process at the row level is achieved by means of the reindex() method. The reindex method has the capability to rearrange the row values as per the sequence associated in the index and when a new index values is inserted in the sequence then all values for that particular row will be filled with None values. Along with its core capability the reindexing function offers a wide set of functionalities.
Syntax:
DataFrame.reindex(self, labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)
Parameters:
Parameter | Description |
labels | These represent the new labels and indexes that conform to the axis value which is mentioned in the axis. |
index | This is an alternative to the argument axis (mapper , axis=0), here index represents the rows of the dataframe. So every rename values which are mentioned here will be applied to the rows of the dataframe. |
columns | This is again an another alternative to the argument axis (mapper , axis=1), Here columns as the name suggests it represents the columns of the dataframe. So every rename values which are mentioned here will be applied to the column names of the dataframe. |
axis | This argument represents the column or the axis upon which the Rename() function needs to be applied on. The value specified in this argument represents either a column position or a row position in the dataframe. To achieve this capability to flexibly travel over a dataframe the axis value is framed on below means {index (0), columns (1)}. Here mentioning the value of 0 to axis argument fills the rename values for each and every row in the dataframe, whereas mentioning the value of 1 in the dataframe fills the replacement values for all the columns in the dataframe. |
copy | Refers whether the data in the dataframe needs to be copied along with the operation which is been performed. |
method | When any of the data need to be filled in the reindexeddataframe then the method argument is used.
|
level | If the axis is a MultiIndex (hierarchical), count along a particular level. |
fill_value | All the missing value will be filled with the value mentioned here. This is a scalar entity and the default value is np.NaN. |
tolerance | For inexact matches this represents the maximum distance between original and new labels. |
Examples of Pandas DataFrame.reindex
Given below are the examples mentioned:
Example #1
Code:
import pandas as pd
import numpy as np
index = ['element1', 'element2', 'element3', 'element4']
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1', np.nan,'Emp3','Emp4'],
'Employee_Name' : ['Arun', 'selva', np.nan, 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', np.nan]}, index=index)
print(" THE CORE DATAFRAME BEFORE REINDEX OPERATION ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER REINDEX OPERATION ")
print(Core_Dataframe.reindex(['element2','Row2','element4','element3']))
print("")
Output:
Explanation:
- In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe are inserted along with their column names.
- Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds details like employee number, Employee name and employee department.
- The reindex method is used to reindex all the row values with a new or rearranged index value and print the updated dataframe onto the console.
Example #2
Code:
import pandas as pd
import numpy as np
index = ['element1', 'element2', 'element3', 'element4','element5','element6']
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, 1, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, 1, 29],
'E' : [5, 10, 15, np.nan, 25, 30]},index=index)
print(" THE CORE DATAFRAME BEFORE REINDEX OPERATION ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER REINDEX OPERATION ")
print(Core_Dataframe.reindex(['element5','element3','element4','element3','element2','element6']))
print("")
Output:
Explanation:
- In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe are inserted along with their column names.
- Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds random set of numbers.
- The reindex method is used to reindex all the row values with a new or rearranged index value and print the updated dataframe onto the console.
Example #3
Code:
import pandas as pd
import numpy as np
index = ['element1','element2','element3', 'element4','element5','element6','element7']
Core_Dataframe = pd.DataFrame( {
'name': ['Alan Xavier', 'Annabella', 'Janawong', 'Yistien', 'Robin sheperd', 'Amalapaul', 'Nori'],
'city': ['california', 'Toronto', 'ontario', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [51, 38, 23, 64, 18, 31, 47],
'py-score': [82.0, 73.0, 81.0, 30.0, 48.0, 61.0, 84.0] },index=index)
print(" THE CORE DATAFRAME BEFORE REINDEX OPERATION ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER REINDEX OPERATION ")
print(Core_Dataframe.reindex(['element5','element3','element4','element1','element2','element6','row7']))
print("")
Output:
Explanation:
- In this example the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe are inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console.
- We can notice at this instance the dataframe holds random people information and the py_score value of those people. The key columns used in this dataframe are name, age, city and py-score value.
- The reindex method is used to reindex all the row values with a new or rearranged index value and print the updated dataframe onto the console.
Conclusion
We can take in from the above examples on how precisely the reindex() operates in rearranging the index values associated to a dataframe.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.reindex” was beneficial to you. You can view EDUCBA’s recommended articles for more information.