1

Having 2 columns where i have to update the third column based on the conditional statement between 2 columns. How i can use the same , i have tried but the case is not working.

We need to check for the condition if Col1 is having value but col2 is blank.

Input Data:

col1      col2         col3

azb225   AS277
Dzb555   
NZb777   NZb777
ZQS285   
NBC605   NZ3385

Output Expected:

col1      col2         col3

azb225   AS277        Available
Dzb555                Not Available
NZb777   NZb777       Available
ZQS285                Not Available
                      Available
NBC605   NZ3385       Available

code i have been using :

df['col3']=df.apply(lambda x:'Not Available' if (x['col1'].notna().all(axis=1)) and (x['col2'].isna().all(axis=1)) else 'Available',1)

But the above code is not working in this case.

Please Suggest.

1 Answer 1

1

Use numpy.where:

#if empty strings instead missing values
df = df.replace('', np.nan)
print (df)
     col1    col2
0  azb225   AS277
1  Dzb555     NaN
2  NZb777  NZb777
3  ZQS285     NaN
4     NaN     NaN
5  NBC605  NZ3385

df['col3']= np.where(df['col1'].notna() & df['col2'].isna(), 'Not Available','Available')

print (df)
     col1    col2           col3
0  azb225   AS277      Available
1  Dzb555     NaN  Not Available
2  NZb777  NZb777      Available
3  ZQS285     NaN  Not Available
4     NaN     NaN      Available
5  NBC605  NZ3385      Available
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.