w3resource

Pandas: Select all rows which not appears in a list

Pandas Filter: Exercise-18 with Solution

Write a Pandas program to filter those records which not appears in a given list from world alcohol consumption dataset.

Test Data:

   Year       WHO region                Country Beverage Types  Display Value
0  1986  Western Pacific               Viet Nam           Wine           0.00
1  1986         Americas                Uruguay          Other           0.50
2  1985           Africa           Cte d'Ivoire           Wine           1.62
3  1986         Americas               Colombia           Beer           4.27
4  1987         Americas  Saint Kitts and Nevis           Beer           1.98   

Sample Solution:

Python Code :

import pandas as pd
# World alcohol consumption data
new_w_a_con = pd.read_csv('world_alcohol.csv')
print("World alcohol consumption sample data:")
print(new_w_a_con.head())
print("\nSelect all rows which not appears in a given list:")
who_region = ["Africa", "Eastern Mediterranean", "Europe"]
flt_wine = ~new_w_a_con["WHO region"].isin(who_region)
print(new_w_a_con[flt_wine])

Sample Output:

World alcohol consumption sample data:
   Year       WHO region      ...      Beverage Types Display Value
0  1986  Western Pacific      ...                Wine          0.00
1  1986         Americas      ...               Other          0.50
2  1985           Africa      ...                Wine          1.62
3  1986         Americas      ...                Beer          4.27
4  1987         Americas      ...                Beer          1.98

[5 rows x 5 columns]

Select all rows which not appears in a given list:
    Year       WHO region      ...      Beverage Types Display Value
0   1986  Western Pacific      ...                Wine          0.00
1   1986         Americas      ...               Other          0.50
3   1986         Americas      ...                Beer          4.27
4   1987         Americas      ...                Beer          1.98
5   1987         Americas      ...               Other          0.00
8   1986         Americas      ...             Spirits          1.55
11  1989         Americas      ...                Beer          0.62
12  1985  Western Pacific      ...                Beer          0.00
14  1985  Western Pacific      ...             Spirits          0.05
16  1984         Americas      ...                Wine          0.06
20  1986  South-East Asia      ...                Wine          0.00
21  1989         Americas      ...             Spirits          4.51
28  1987  Western Pacific      ...                Beer          0.11
31  1986  Western Pacific      ...                Wine          0.00
35  1985         Americas      ...             Spirits          2.24
43  1984  Western Pacific      ...                Wine          0.03
46  1987         Americas      ...             Spirits          2.26
47  1986         Americas      ...               Other          0.04
48  1987         Americas      ...                Beer          0.70
54  1984         Americas      ...             Spirits          1.81
55  1989         Americas      ...                Wine          0.04
56  1987  Western Pacific      ...                Wine          0.00
61  1984  Western Pacific      ...             Spirits          0.08
62  1987         Americas      ...               Other          0.00
64  1989         Americas      ...                Beer          1.26
74  1986         Americas      ...             Spirits          2.06
78  1989         Americas      ...               Other          0.00
84  1986  South-East Asia      ...               Other          0.00
86  1986         Americas      ...                Wine          1.83
97  1984  South-East Asia      ...                Wine          0.00
99  1985  South-East Asia      ...                Wine          0.00

[31 rows x 5 columns]

Click to download world_alcohol.csv

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to filter those records where WHO region matches with multiple values (Africa, Eastern Mediterranean, Europe) from world alcohol consumption dataset.

Next: Write a Pandas program to filter all records where the average consumption of beverages per person from .5 to 2.50 in world alcohol consumption dataset.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/pandas/filter/pandas-filter-exercise-18.php