import pandas as pd
dic = {'A': [np.nan, 4, np.nan, 4], 'B': [9, 2, 5, 3], 'C': [0, 0, 5, 3]}
df = pd.DataFrame(dic)
df
If I have data like below
A B C
0 NaN 9 0
1 4.0 2 0
2 NaN 5 5
3 4.0 3 3
I want to select the raw that column A is NaN
and replace column B's value with np.nan as follows.
A B C
0 NaN NaN 0
1 4.0 2.0 0
2 NaN NaN 5
3 4.0 3.0 3
I tried to do df[df.A.isna()]["B"]=np.nan
, but it didn't work.
According to this page, I should to select data by df.iloc
. But the problem is that if df have numerous rows, I can't select data by input index.
Option 1
You were pretty close actually. Use pd.Series.isnull
on A
and assign values to B
using df.loc
:
df.loc[df.A.isnull(), 'B'] = np.nan
df
A B C
0 NaN NaN 0
1 4.0 2.0 0
2 NaN NaN 5
3 4.0 3.0 3
Option 2np.where
:
df['B'] = np.where(df.A.isnull(), np.nan, df.B)
df
A B C
0 NaN NaN 0
1 4.0 2.0 0
2 NaN NaN 5
3 4.0 3.0 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With