Data Filtering

cereal = pd.read_csv('data/cereal.csv')
cereal.head()
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 3 5 15 2.0 8.0 8 135 0 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 4 0 140 14.0 8.0 0 330 25 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 2 2 200 1.0 14.0 8 1 25 3 1.0 0.75 34.384843

Conditions

cereal['protein'] > 4
0     False
1     False
2     False
      ...  
74    False
75    False
76    False
Name: protein, Length: 77, dtype: bool
cereal[cereal['protein'] > 4]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
11 Cheerios G Cold 110 6 2 290 2.0 17.0 1 105 25 1 1.0 1.25 50.764999
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392
67 Special K K Cold 110 6 0 230 1.0 16.0 3 55 25 1 1.0 1.00 53.131324
cereal[cereal['protein'] == 4]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 4 0 140 14.0 8.0 0 330 25 3 1.0 0.50 93.704912
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
44 Muesli Raisins; Dates; & Almonds R Cold 150 4 3 95 3.0 16.0 11 170 25 3 1.0 1.00 37.136863
45 Muesli Raisins; Peaches; & Pecans R Cold 150 4 3 150 3.0 16.0 11 170 25 3 1.0 1.00 34.139765
56 Quaker Oat Squares Q Cold 100 4 1 135 2.0 14.0 6 110 25 3 1.0 0.50 49.511874

8 rows × 16 columns

cereal[cereal['mfr'] == 'Q']
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
1 100% Natural Bran Q Cold 120 3 5 15 2.0 8.0 8 135 0 3 1.0 1.00 33.983679
10 Cap'n'Crunch Q Cold 120 1 2 220 0.0 12.0 12 35 25 2 1.0 0.75 18.042851
35 Honey Graham Ohs Q Cold 120 1 2 220 1.0 12.0 11 45 25 2 1.0 1.00 21.871292
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
55 Puffed Wheat Q Cold 50 2 0 0 1.0 10.0 0 50 0 3 0.5 1.00 63.005645
56 Quaker Oat Squares Q Cold 100 4 1 135 2.0 14.0 6 110 25 3 1.0 0.50 49.511874
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392

8 rows × 16 columns

Multiple Condition Filtering - “and”

cereal[cereal['protein'] >= 4]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 4 0 140 14.0 8.0 0 330 25 3 1.0 0.50 93.704912
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
56 Quaker Oat Squares Q Cold 100 4 1 135 2.0 14.0 6 110 25 3 1.0 0.50 49.511874
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392
67 Special K K Cold 110 6 0 230 1.0 16.0 3 55 25 1 1.0 1.00 53.131324

11 rows × 16 columns

cereal[cereal['protein'] <= 5]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 3 5 15 2.0 8.0 8 135 0 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
74 Wheat Chex R Cold 100 3 1 230 3.0 17.0 3 115 25 1 1.0 0.67 49.787445
75 Wheaties G Cold 100 3 1 200 3.0 17.0 3 110 25 1 1.0 1.00 51.592193
76 Wheaties Honey Gold G Cold 110 2 1 200 1.0 16.0 8 60 25 1 1.0 0.75 36.187559

75 rows × 16 columns

cereal[cereal['protein'] >= 4]
cereal[cereal['protein'] <= 5]


cereal[(cereal['protein'] >= 4) & (cereal['protein'] <= 5)]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 4 0 140 14.0 8.0 0 330 25 3 1.0 0.50 93.704912
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
45 Muesli Raisins; Peaches; & Pecans R Cold 150 4 3 150 3.0 16.0 11 170 25 3 1.0 1.00 34.139765
56 Quaker Oat Squares Q Cold 100 4 1 135 2.0 14.0 6 110 25 3 1.0 0.50 49.511874
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392

9 rows × 16 columns

404 image
cereal[(cereal['mfr'] == 'Q') & (cereal['protein'] > 4)]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392

Multiple Condition Filtering - “or”

cereal[(cereal['mfr'] == 'Q') | (cereal['protein'] > 4)]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
1 100% Natural Bran Q Cold 120 3 5 15 2.0 8.0 8 135 0 3 1.0 1.00 33.983679
10 Cap'n'Crunch Q Cold 120 1 2 220 0.0 12.0 12 35 25 2 1.0 0.75 18.042851
11 Cheerios G Cold 110 6 2 290 2.0 17.0 1 105 25 1 1.0 1.25 50.764999
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
56 Quaker Oat Squares Q Cold 100 4 1 135 2.0 14.0 6 110 25 3 1.0 0.50 49.511874
57 Quaker Oatmeal Q Hot 100 5 2 0 2.7 1.0 1 110 0 1 1.0 0.67 50.828392
67 Special K K Cold 110 6 0 230 1.0 16.0 3 55 25 1 1.0 1.00 53.131324

10 rows × 16 columns

404 image

Tilde

cereal['protein'] > 4
0     False
1     False
2     False
      ...  
74    False
75    False
76    False
Name: protein, Length: 77, dtype: bool
(cereal['protein'] > 4).head()
0    False
1    False
2    False
3    False
4    False
Name: protein, dtype: bool


Tilde converts all the True values to False and all the False values, to True.

(~(cereal['protein'] > 4)).head()
0    True
1    True
2    True
3    True
4    True
Name: protein, dtype: bool
cereal[~(cereal['protein'] > 4)]
name mfr type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
0 100% Bran N Cold 70 4 1 130 10.0 5.0 6 280 25 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 3 5 15 2.0 8.0 8 135 0 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 4 1 260 9.0 7.0 5 320 25 3 1.0 0.33 59.425505
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
74 Wheat Chex R Cold 100 3 1 230 3.0 17.0 3 115 25 1 1.0 0.67 49.787445
75 Wheaties G Cold 100 3 1 200 3.0 17.0 3 110 25 1 1.0 1.00 51.592193
76 Wheaties Honey Gold G Cold 110 2 1 200 1.0 16.0 8 60 25 1 1.0 0.75 36.187559

74 rows × 16 columns

Let’s apply what we learned!