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 # Create a Series from a list # Replace all the even numbers with zero print("Original Series:") print("Resultant Series after masking:") |
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 import pandas as pds # A callable that is called once which returns a new Series with # Create a Series # Mask the values greater than 5 with mean value |
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 # Create a pandas Series # Replace 1s with 0s |
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 |