Python for Data Science: Chapter 5: NumPy and Pandas Libraries

Pandas: Reindexing

Python library

This functionality is used to create new object with conformed to new index.

Reindexing

• This functionality is used to create new object with conformed to new index. Following Python code illustrates the concept of reindexing.

 

Python code

import pandas as pd

data = [30,20,10,40]

x = pd.Series(data, index=["c","b","a","d"], dtype = float)

print(x)

#calling reindex on series

print("‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒")

print("        Reindexing             ")

print("‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒")

y = x.reindex(["a","b","c","d"])

print(y)

Output


In [10]:

c      30.0

b      20.0

a      10.0

d     40.0

dtype: float64

‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒

            Reindexing

‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒

a       10.0

b       20.0

c       30.0

d      40.0

dtype: float64

In [11]:

 

Syntax of reindex method is

  dataframe.reindex(keys, method, copy, level, fill_value, limit, tolerance)

 keys: The list or string containing row indexes or column labels.

Method: The default method is None. It specifies the method to use when filling holes in the indexes. The methods are 'backfill', 'bfill', 'pad', 'ffil', 'nearest'

Copy: This is an optional parameter. The values can be True or False. The default is True. Whether to return a new object (a copy) when all the new indexes are the same as the old.

 level: The values can be Number or label. This parameter is optional.

•  fill_value : This function specifies the value to use for missing values. The default value is NaN. This is an optional parameter.

limit: This function specifies the maximum size gap when forward ‒ or backfilling. This is optional parameter, default is None.

tolerance: This function specifies maximum size (in absolute numeric distance) to fill for inexact matches, When forward ‒ or backfilling,

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - Pandas: Reindexing


Python for Data Science: Chapter 5: NumPy and Pandas Libraries



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation