Given the following python pandas dataframe:
| province | district |
|---|---|
| Total | example |
| NaN | other |
| Other | NaN |
| NaN | example |
| Result | example |
| NaN | example |
If the province column is NaN and the value for that row is ‘example’, I want to fill the province gap with ‘example’. The rest of the rows stay as they are.
DataFrame result:
| province | district |
|---|---|
| Total | example |
| NaN | other |
| Other | NaN |
| example | example |
| Result | example |
| example | example |
>Solution :
You can use .fillna() conditionally with np.where:
df["province"] = np.where(
df["district"] == "example",
df["province"].fillna(value="example"),
df["province"]
)