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
python
# 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
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
python
# 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)
Example 3 - Change elements through replace() method
python
# 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)
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on