I want to change NaN values from one column based on the value of another column.
First table
| A | B |
|---|---|
| 2 | 3 |
| 0 | NaN |
| 2 | NaN |
| 4 | 2 |
Targeted table
| A | B |
|---|---|
| 2 | 3 |
| 0 | 0 |
| 2 | NaN |
| 4 | 2 |
So I want to change NaN to 0, if the value of column A in that row is also 0.
The python code that I create:
for i in range(len(df)):
if df.loc[i,'A'] == 0 and math.isnan(df.loc[i,'B']) = True:
df.loc[i,'B'] = 0
But this code seems so slow, because I want to do this for 20 million rows. Is there any faster way to do this?