economatic
economatic

Reputation: 9

df.loc with NaN is not changing anything

What am I doing wrong here? I am trying to change my data frame but nothing is happening?

df.loc[(df['var1'] == np.nan) & (df['var2'] == np.nan) & (df['var3'] >=0), 'var2'] = 0

Upvotes: 1

Views: 3177

Answers (2)

user13233820
user13233820

Reputation:

I am guessing that you want to fill nan values with zero and for that, you can use:

df['DataFrame Column'] = df['DataFrame Column'].fillna(0)

or

df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 0)

If this was not what you asked, please comment!!

Upvotes: 0

Amir saleem
Amir saleem

Reputation: 1496

df['var1'] == np.nan Never been True, Because np.nan not equal to anything not himself ! . you can check np.nan value as df.var1.isna() or in single value case value is np.nan. if x is single value np.nan, and you try: x == np.nan the result is False, you should do x is np.nan.

Solution

df.iloc[(df[['var1', 'var2']].isna().all(1)) & (df.var3 >= 0), 'var2'] = 0

Upvotes: 1

Related Questions