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.
| Old |
New |
| type |
value |
| a |
1 |
| a |
NaN |
| b |
3 |
| b |
NaN |
|
| type |
value |
| a |
1 |
| a |
1 |
| b |
3 |
| b |
NaN |
|
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)