Reputation: 13
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
Upvotes: 1
Views: 110
Reputation: 863741
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
Upvotes: 3