Shankar Guru
Shankar Guru

Reputation: 1161

Check for null before applying a logic in pandas

I am currently performing below operation. I need to check if x is null before performing any mathematical operation on the column in a dataframe. Please let me know how it can be achieved ?

df['converted_value'] = df['main_value'].apply(lambda x: (float(x)/10000) * 350000)

Upvotes: 0

Views: 93

Answers (2)

Cameron Oliver
Cameron Oliver

Reputation: 91

When you say "null", I'm not sure if you mean None or NaN (as in np.nan, which is a float).

If you mean None:

df['converted_value'] = df['main_value'].apply(lambda x: (x/10000) * 350000 if x is not None else None)

If you mean NaN (np.nan), then what you have should return NaN for NaN input already, but, as suggested in the comments, you could do a column operation instead in that case.

Upvotes: 1

jezrael
jezrael

Reputation: 862801

Your function working with NaNs very nice, faster is vectorized alternative:

df['converted_value'] = (df['main_value'] /10000) * 350000)

One possible solution is filtering only non missing values and apply function:

mask = df['main_value'].notna()
f = lambda x: function_not_working_with_nan(x)
df.loc[mask, 'converted_value'] = df.loc[mask, 'main_value'].apply(f)

Upvotes: 1

Related Questions