Replacing Pandas Series Values

Overview:

  • The mask() method accepts a Boolean condition and replaces each element of a pandas Series that satisfies the condition with the given value. More sophisticated replacement strategies shall be implemented using the callable parameter.

  • The replace() method replaces the occurrences of specified value with a given value. Regular expressions are supported for the string elements present in a pandas Series.

Example 1 - Using mask() method to replace elements satisfying a Boolean condition:

# Example Python program that masks specific values in a pandas Series
# that matches a condition
import pandas as pds 

# Create a Series from a list
vals     = [1, 2, 3, 4, 5]
series     = pds.Series(data = vals)

# Replace all the even numbers with zero
resultant = series.mask(series%2 == 0, 0)

print("Original Series:")
print(series)

print("Resultant Series after masking:")
print(resultant)

Output:

Original Series:

0    1

1    2

2    3

3    4

4    5

dtype: int64

Resultant Series after masking:

0    1

1    0

2    3

3    0

4    5

dtype: int64

Example 2 - Using a callable to implement a replacement strategy:

# Example Python program that uses a callable
# to mask the values of a pandas Series

import pandas as pds 

# A callable that is called once which returns a new Series with
# masked results
def meanReplace(data):
    d1 = data.copy(deep=True)
    for i in d1:
        if i > 5:
            d1[i] = sum(data) / len(data)
    return d1            

# Create a Series
s1 = pds.Series(data = range(0, 10))
print("Original Series:")
print(s1)

# Mask the values greater than 5 with mean value
s2 = s1.mask(s1 > 5, meanReplace(s1)) 
print("Masked Series using a callable:")
print(s2)

Output:

Original Series:

0    0

1    1

2    2

3    3

4    4

5    5

6    6

7    7

8    8

9    9

dtype: int64

Masked Series using a callable:

0    0.0

1    1.0

2    2.0

3    3.0

4    4.0

5    5.0

6    4.5

7    4.5

8    4.5

9    4.5

dtype: float64

Example 3 - Change elements through replace() method:

# Example Python program that replaces
# specific occurrences of a value with a 
# given value
import pandas as pds

# Create a pandas Series
values = [1, 3, 2, 1, 5, 2, 4, 1]
series = pds.Series(data = values)
print("Original data:")
print(series)

# Replace 1s with 0s
print("New data:")
newSeries = series.replace(1, 0)
print(newSeries)

 

Output:

Original data:

0    1

1    3

2    2

3    1

4    5

5    2

6    4

7    1

dtype: int64

New data:

0    0

1    3

2    2

3    0

4    5

5    2

6    4

7    0

dtype: int64


Copyright 2023 © pythontic.com