0

I want to transform a pandas column that contains Nan from string to float. This is the code I tried but it keeps returning me an invalid syntax error

data.VAL_DEAL=data.VAL_DEAL.apply(lambda x: float(x.replace(",","")) if math.isnan(x)!=True)
1
  • You can just do df['VAL_DEAL'] = pd.to_numeric(df['VAL_DEAL'], errors='coerce'), there is no need to use apply here Commented Jun 27, 2018 at 9:23

1 Answer 1

2

The following lambda expression should work:

lambda x: float(x.replace(",","") if not math.isnan(x) else x)

Note the mandatory else-part. This assumes that you want the nan's unchanged. See the docs on Conditional Expressions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.