I have a pandas data frame, sample
, with one of the columns called PR
to which am applying a lambda function as follows:
sample['PR'] = sample['PR'].apply(lambda x: NaN if x < 90)
I then get the following syntax error message:
sample['PR'] = sample['PR'].apply(lambda x: NaN if x < 90)
^
SyntaxError: invalid syntax
What am I doing wrong?
You need
mask
:Another solution with
loc
andboolean indexing
:Sample:
EDIT:
Solution with
apply
:Timings
len(df)=300k
:You need to add else in your lambda function Because you are telling what to do in case your condition(here x < 90) is met, but you are not telling what to do if in case the condition is not met.
sample['PR'] = sample['PR'].apply(lambda x: 'NaN' if x < 90 else x)