Skip to main content

Be a Problem Solver

Replacing Values in a Pandas DataFrame Using a Mask

To update values in a pandas DataFrame, we can use a mask to select rows based on a condition. In the following example, a DataFrame with type and value columns is used. Furthermore, we’d like to replace NaN values in the value column with the previous value, but only for rows where the type column equals a.

OldNew
typevalue
a1
aNaN
b3
bNaN
typevalue
a1
a1
b3
bNaN

To achieve this, we can use the mask(cond, other) method, which replaces values where cond evaluates to True with those specified in other.

import pandas as pd

data = pd.DataFrame({"type": ["a", "a", "b", "b"], "value": [1, None, 3, None]})
data["value"] = data["value"].mask(data["type"] == "a", data["value"].ffill())
print(data)