1

I'm new to python/panda. This dataframe is part of a lab exercise.

Following is the dataframe. The result has 20 rows and 384 columns.

           country almond angelica anise anise_seed apple apple_brandy /
55620  Switzerland     No       No    No         No    No           No   
55621  Switzerland    Yes       No    No         No   Yes           No   
55622  Switzerland     No       No    No         No    No           No   
55623  Switzerland     No       No    No         No    No           No   
55624  Switzerland     No       No    No         No    No           No   
55625  Switzerland     No       No    No         No    No           No   
55626  Switzerland     No       No    No         No    No           No   
55627  Switzerland     No       No    No         No    No           No   
55628  Switzerland    Yes       No    No         No    No           No

I want to remove all the columns where value is 'No' and create a subset of this dataframe without these columns. From the above data set it means only columns 'almond' and 'apple' have a value other than 'No'. So the subset should look like,

           country almond apple 
55620  Switzerland     No    No
55621  Switzerland    Yes   Yes
55622  Switzerland     No    No
55623  Switzerland     No    No
55624  Switzerland     No    No
55625  Switzerland     No    No
55626  Switzerland     No    No
55627  Switzerland     No    No   
55628  Switzerland    Yes    No
1
  • Can you show us what you have tried so far? Commented May 11, 2020 at 11:04

1 Answer 1

3

First check by eq for equality and then test if at least one matched value in DataFrame.any:

df = df.loc[:, df.ne('No').any()]
print (df)
           country almond apple
55620  Switzerland     No    No
55621  Switzerland    Yes   Yes
55622  Switzerland     No    No
55623  Switzerland     No    No
55624  Switzerland     No    No
55625  Switzerland     No    No
55626  Switzerland     No    No
55627  Switzerland     No    No
55628  Switzerland    Yes    No
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked! May I know where/how 'eq' was used here?
@pradeept - Sure, it is better for easy chain by anoter functions and also better readable, because less ()

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.