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)
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.
df['VAL_DEAL'] = pd.to_numeric(df['VAL_DEAL'], errors='coerce'), there is no need to useapplyhere