Fuad Ak
Fuad Ak

Reputation: 158

Replace NaN values in DataFrame

I have a dataframe:

 df5=pd.DataFrame( {'a':[1,2,3,4],
                    'b':[5,np.nan,np.nan,8] },
                     index=pd.date_range('7-23-2020', periods=4, name='date'))

            a    b  
date                
2020-07-23  1   5.0 
2020-07-24  2   NaN 
2020-07-25  3   NaN 
2020-07-26  4   8.0 

I want to select np.nan values in 'b' column and replace with corresponding vales in 'a'. This my code:

df5.loc[df5['B'].isna(),'B']=df5.loc[df5['B'].isna(),'A']*100

Is this code a good practice?

Upvotes: 1

Views: 72

Answers (1)

EMiller
EMiller

Reputation: 837

A better solution would be to use:

df5['b'] = df5.b.fillna(df5.a*100)

Upvotes: 3

Related Questions