TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas Reindex

Pandas Reindex with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Reindex

The main task of the Pandas reindex is to conform DataFrame to a new index with optional filling logic and to place NA/NaN in that location where the values are not present in the previous index. It returns a new object unless the new index is produced as an equivalent to the current one, and the value of copy becomes False.

Reindexing is used to change the index of the rows and columns of the DataFrame. We can reindex the single or multiple rows by using the reindex() method. Default values in the new index are assigned NaN if it is not present in the DataFrame.

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 is an optional parameter that refers to the new labels or the index to conform to the axis that is specified by the 'axis'.

index, columns : It is also an optional parameter that refers to the new labels or the index. It generally prefers an index object for avoiding the duplicate data.

axis : It is also an optional parameter that targets the axis and can be either the axis name or the numbers.

method: It is also an optional parameter that is to be used for filling the holes in the reindexed DataFrame. It can only be applied to the DataFrame or Series with a monotonically increasing/decreasing order.

None: It is a default value that does not fill the gaps.

pad / ffill: It is used to propagate the last valid observation forward to the next valid observation.

backfill / bfill: To fill the gap, It uses the next valid observation.

nearest: To fill the gap, it uses the next valid observation.

copy: Its default value is True and returns a new object as a boolean value, even if the passed indexes are the same.

level : It is used to broadcast across the level, and match index values on the passed MultiIndex level.

fill_value : Its default value is np.NaN and used to fill existing missing (NaN) values. It needs any new element for successful DataFrame alignment, with this value before computation.

limit : It defines the maximum number of consecutive elements that are to be forward or backward fill.

tolerance : It is also an optional parameter that determines the maximum distance between original and new labels for inexact matches. At the matching locations, the values of the index should most satisfy the equation abs(index[indexer] ? target) <= tolerance.

Returns :

It returns reindexed DataFrame.

Example 1:

The below example shows the working of reindex() function to reindex the dataframe. In the new index,default values are assigned NaN in the new index that does not have corresponding records in the DataFrame.

Note: We can use fill_value for filling the missing values.

import pandas as pd

# Create dataframe
info = pd.DataFrame({"P":[4, 7, 1, 8, 9], 
                   "Q":[6, 8, 10, 15, 11], 
                   "R":[17, 13, 12, 16, 14], 
                   "S":[15, 19, 7, 21, 9]}, 
                   index =["Parker", "William", "Smith", "Terry", "Phill"]) 

# Print dataframe
info

Output:

         A    B    D    E
Parker	NaN  NaN  NaN  NaN
William	NaN  NaN  NaN  NaN
Smith	NaN  NaN  NaN  NaN
Terry	NaN  NaN  NaN  NaN
Phill	NaN  NaN  NaN  NaN

Now, we can use the dataframe.reindex() function to reindex the dataframe.

# reindexing with new index values 
info.reindex(["A", "B", "C", "D", "E"])

Output:

	P	Q	R	S
A	NaN	NaN	NaN	NaN
B	NaN	NaN	NaN	NaN
C	NaN	NaN	NaN	NaN
D	NaN	NaN	NaN	NaN
E	NaN	NaN	NaN	NaN

Notice that the new indexes are populated with NaN values. We can fill in the missing values using the fill_value parameter.

# filling the missing values by 100 
info.reindex(["A", "B", "C", "D", "E"], fill_value =100)

Output:

	P	Q	R	S
A	100	100	100	100
B	100	100	100	100
C	100	100	100	100
D	100	100	100	100
E	100	100	100	100

Example 2:

This example shows the working of reindex() function to reindex the column axis.

# importing pandas as pd
importpandas as pd
  
# Creating the first dataframe  
info1 =pd.DataFrame({"A":[1, 5, 3, 4, 2], 
                    "B":[3, 2, 4, 3, 4], 
                    "C":[2, 2, 7, 3, 4], 
                    "D":[4, 3, 6, 12, 7]}) 
# reindexing the column axis with 
# old and new index values 
info.reindex(columns =["A", "B", "D", "E"])

Output:

        A     B    D    E
Parker	NaN  NaN  NaN  NaN
William	NaN  NaN  NaN  NaN
Smith	NaN  NaN  NaN  NaN
Terry	NaN  NaN  NaN  NaN
Phill	NaN  NaN  NaN  NaN

Notice that NaN values are present in the new columns after reindexing, we can use the argument fill_value to the function for removing the NaN values.

# reindex the columns 
# fill the missing values by 25 
info.reindex(columns =["A", "B", "D", "E"], fill_value =37)

Output:

        A   B   D   E
Parker	37  37  37  37
William	37  37  37  37
Smith	37  37  37  37
Terry	37  37  37  37
Phill	37  37  37  37

Next TopicReset Index




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf