0

Trying to create the new column in my dataframe based on the below condition:

dataFrame01['final'] = dataFrame01.apply(lambda x: x['Name'] if x['Eval'] == 'NAN' else x['Eval'], axis=1)

but every time only ELSE block is getting executed I mean values from else condition as getting populated but not from IF conditions. Please help and let me know what mistake I am doing here.

3
  • Difficult to answer unless you share the actual data in the pandas dataframe. Commented Apr 29, 2020 at 12:46
  • For the next time, please spend a minute to see how to properly format your code (done it for you this time). Also, this is a standard pandas question, and it has nothing to do with machine-learning - kindly do not spam irrelevant tags (removed). Commented Apr 29, 2020 at 12:50
  • is your data string NAN or np.nan? Also check for leading/trailing white spaces. Commented Apr 29, 2020 at 12:58

1 Answer 1

1

Hard to say without seeing the data. It appears as though the below expression is not getting evaluated.

x['Eval'] == 'NAN'

As a hunch, check that you are specifying your NaN correctly. In Pandas, missing values are typically specified as np.nan. One way to evaluate missing values in Pandas is with pd.isnull(). Thus, the code would look something like this:

dataFrame01['final'] = dataFrame01.apply(lambda x: x['Name'] if pd.isnull(x['Eval']) else x['Eval'], axis=1)
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.