You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udemy-ML/03-Pandas/02-Conditional-Filtering.ipynb

190 KiB

<html> <head> </head>

___

Copyright by Pierian Data Inc. For more information, visit us at www.pieriandata.com

Conditional Filtering

Imports

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.read_csv('tips.csv')
In [5]:
df.head()
Out[5]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
0 16.99 1.01 Female No Sun Dinner 2 8.49 Christy Cunningham 3560325168603410 Sun2959
1 10.34 1.66 Male No Sun Dinner 3 3.45 Douglas Tucker 4478071379779230 Sun4608
2 21.01 3.50 Male No Sun Dinner 3 7.00 Travis Walters 6011812112971322 Sun4458
3 23.68 3.31 Male No Sun Dinner 2 11.84 Nathaniel Harris 4676137647685994 Sun5260
4 24.59 3.61 Female No Sun Dinner 4 6.15 Tonya Carter 4832732618637221 Sun2251

Conditions

In [12]:
# df['total_bill'] > 30
In [10]:
bool_series = df['total_bill'] > 30
In [11]:
df[bool_series]
Out[11]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
39 31.27 5.00 Male No Sat Dinner 3 10.42 Mr. Brandon Berry 6011525851069856 Sat6373
44 30.40 5.60 Male No Sun Dinner 4 7.60 Todd Cooper 503846761263 Sun2274
47 32.40 6.00 Male No Sun Dinner 4 8.10 James Barnes 3552002592874186 Sun9677
52 34.81 5.20 Female No Sun Dinner 4 8.70 Emily Daniel 4291280793094374 Sun6165
56 38.01 3.00 Male Yes Sat Dinner 4 9.50 James Christensen DDS 349793629453226 Sat8903
59 48.27 6.73 Male No Sat Dinner 4 12.07 Brian Ortiz 6596453823950595 Sat8139
83 32.68 5.00 Male Yes Thur Lunch 2 16.34 Daniel Murphy 5356177501009133 Thur8801
85 34.83 5.17 Female No Thur Lunch 4 8.71 Shawna Cook 6011787464177340 Thur7972
95 40.17 4.73 Male Yes Fri Dinner 4 10.04 Aaron Bentley 180026611638690 Fri9628
102 44.30 2.50 Female Yes Sat Dinner 3 14.77 Heather Cohen 379771118886604 Sat6240
112 38.07 4.00 Male No Sun Dinner 3 12.69 Jeff Lopez 3572865915176463 Sun591
141 34.30 6.70 Male No Thur Lunch 6 5.72 Steven Carlson 3526515703718508 Thur1025
142 41.19 5.00 Male No Thur Lunch 5 8.24 Eric Andrews 4356531761046453 Thur3621
156 48.17 5.00 Male No Sun Dinner 6 8.03 Ryan Gonzales 3523151482063321 Sun7518
167 31.71 4.50 Male No Sun Dinner 4 7.93 Michael Lawson 3566285921227119 Sun3719
170 50.81 10.00 Male Yes Sat Dinner 3 16.94 Gregory Clark 5473850968388236 Sat1954
173 31.85 3.18 Male Yes Sun Dinner 2 15.92 Scott Perez 3577115550328507 Sun9335
175 32.90 3.11 Male Yes Sun Dinner 2 16.45 Nathan Reynolds 370307040837149 Sun5109
179 34.63 3.55 Male Yes Sun Dinner 2 17.32 Brian Bailey 346656312114848 Sun9851
180 34.65 3.68 Male Yes Sun Dinner 4 8.66 James Hebert DDS 676168737648 Sun7544
182 45.35 3.50 Male Yes Sun Dinner 3 15.12 Jose Parsons 4112207559459910 Sun2337
184 40.55 3.00 Male Yes Sun Dinner 2 20.27 Stephen Cox 3547798222044029 Sun5140
187 30.46 2.00 Male Yes Sun Dinner 5 6.09 David Barrett 4792882899700988 Sun9987
197 43.11 5.00 Female Yes Thur Lunch 4 10.78 Brooke Soto 5544902205760175 Thur9313
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
In [13]:
df[df['total_bill']>30]
Out[13]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
39 31.27 5.00 Male No Sat Dinner 3 10.42 Mr. Brandon Berry 6011525851069856 Sat6373
44 30.40 5.60 Male No Sun Dinner 4 7.60 Todd Cooper 503846761263 Sun2274
47 32.40 6.00 Male No Sun Dinner 4 8.10 James Barnes 3552002592874186 Sun9677
52 34.81 5.20 Female No Sun Dinner 4 8.70 Emily Daniel 4291280793094374 Sun6165
56 38.01 3.00 Male Yes Sat Dinner 4 9.50 James Christensen DDS 349793629453226 Sat8903
59 48.27 6.73 Male No Sat Dinner 4 12.07 Brian Ortiz 6596453823950595 Sat8139
83 32.68 5.00 Male Yes Thur Lunch 2 16.34 Daniel Murphy 5356177501009133 Thur8801
85 34.83 5.17 Female No Thur Lunch 4 8.71 Shawna Cook 6011787464177340 Thur7972
95 40.17 4.73 Male Yes Fri Dinner 4 10.04 Aaron Bentley 180026611638690 Fri9628
102 44.30 2.50 Female Yes Sat Dinner 3 14.77 Heather Cohen 379771118886604 Sat6240
112 38.07 4.00 Male No Sun Dinner 3 12.69 Jeff Lopez 3572865915176463 Sun591
141 34.30 6.70 Male No Thur Lunch 6 5.72 Steven Carlson 3526515703718508 Thur1025
142 41.19 5.00 Male No Thur Lunch 5 8.24 Eric Andrews 4356531761046453 Thur3621
156 48.17 5.00 Male No Sun Dinner 6 8.03 Ryan Gonzales 3523151482063321 Sun7518
167 31.71 4.50 Male No Sun Dinner 4 7.93 Michael Lawson 3566285921227119 Sun3719
170 50.81 10.00 Male Yes Sat Dinner 3 16.94 Gregory Clark 5473850968388236 Sat1954
173 31.85 3.18 Male Yes Sun Dinner 2 15.92 Scott Perez 3577115550328507 Sun9335
175 32.90 3.11 Male Yes Sun Dinner 2 16.45 Nathan Reynolds 370307040837149 Sun5109
179 34.63 3.55 Male Yes Sun Dinner 2 17.32 Brian Bailey 346656312114848 Sun9851
180 34.65 3.68 Male Yes Sun Dinner 4 8.66 James Hebert DDS 676168737648 Sun7544
182 45.35 3.50 Male Yes Sun Dinner 3 15.12 Jose Parsons 4112207559459910 Sun2337
184 40.55 3.00 Male Yes Sun Dinner 2 20.27 Stephen Cox 3547798222044029 Sun5140
187 30.46 2.00 Male Yes Sun Dinner 5 6.09 David Barrett 4792882899700988 Sun9987
197 43.11 5.00 Female Yes Thur Lunch 4 10.78 Brooke Soto 5544902205760175 Thur9313
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
In [14]:
df[df['sex'] == 'Male']
Out[14]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
1 10.34 1.66 Male No Sun Dinner 3 3.45 Douglas Tucker 4478071379779230 Sun4608
2 21.01 3.50 Male No Sun Dinner 3 7.00 Travis Walters 6011812112971322 Sun4458
3 23.68 3.31 Male No Sun Dinner 2 11.84 Nathaniel Harris 4676137647685994 Sun5260
5 25.29 4.71 Male No Sun Dinner 4 6.32 Erik Smith 213140353657882 Sun9679
6 8.77 2.00 Male No Sun Dinner 2 4.38 Kristopher Johnson 2223727524230344 Sun5985
7 26.88 3.12 Male No Sun Dinner 4 6.72 Robert Buck 3514785077705092 Sun8157
8 15.04 1.96 Male No Sun Dinner 2 7.52 Joseph Mcdonald 3522866365840377 Sun6820
9 14.78 3.23 Male No Sun Dinner 2 7.39 Jerome Abbott 3532124519049786 Sun3775
10 10.27 1.71 Male No Sun Dinner 2 5.14 William Riley 566287581219 Sun2546
12 15.42 1.57 Male No Sun Dinner 2 7.71 Chad Harrington 577040572932 Sun1300
13 18.43 3.00 Male No Sun Dinner 4 4.61 Joshua Jones 6011163105616890 Sun2971
15 21.58 3.92 Male No Sun Dinner 2 10.79 Matthew Reilly 180073029785069 Sun1878
17 16.29 3.71 Male No Sun Dinner 3 5.43 John Pittman 6521340257218708 Sun2998
19 20.65 3.35 Male No Sat Dinner 3 6.88 Timothy Oneal 6568069240986485 Sat9213
20 17.92 4.08 Male No Sat Dinner 2 8.96 Thomas Rice 4403296224639756 Sat1709
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
24 19.82 3.18 Male No Sat Dinner 2 9.91 Christopher Ross 36739148167928 Sat6236
25 17.81 2.34 Male No Sat Dinner 4 4.45 Robert Perkins 30502930499388 Sat907
26 13.37 2.00 Male No Sat Dinner 2 6.68 Kyle Avery 6531339539615499 Sat6651
27 12.69 2.00 Male No Sat Dinner 2 6.34 Patrick Barber 30155551880343 Sat394
28 21.70 4.30 Male No Sat Dinner 2 10.85 David Collier 5529694315416009 Sat3697
30 9.55 1.45 Male No Sat Dinner 2 4.78 Grant Hall 30196517521548 Sat4099
31 18.35 2.50 Male No Sat Dinner 4 4.59 Danny Santiago 630415546013 Sat4947
34 17.78 3.27 Male No Sat Dinner 2 8.89 Jacob Castillo 3551492000704805 Sat8124
35 24.06 3.60 Male No Sat Dinner 3 8.02 Joseph Mullins 5519770449260299 Sat632
36 16.31 2.00 Male No Sat Dinner 3 5.44 William Ford 3527691170179398 Sat9139
38 18.69 2.31 Male No Sat Dinner 3 6.23 Brandon Bradley 4427601595688633 Sat4056
39 31.27 5.00 Male No Sat Dinner 3 10.42 Mr. Brandon Berry 6011525851069856 Sat6373
40 16.04 2.24 Male No Sat Dinner 3 5.35 Adam Edwards 3544447755679420 Sat8549
41 17.46 2.54 Male No Sun Dinner 2 8.73 David Boyer 3536678244278149 Sun9460
... ... ... ... ... ... ... ... ... ... ... ...
195 7.56 1.44 Male No Thur Lunch 2 3.78 Michael White 4865390263095532 Thur697
196 10.34 2.00 Male Yes Thur Lunch 2 5.17 Eric Martin 30442491190342 Thur9862
199 13.51 2.00 Male Yes Thur Lunch 2 6.76 Joseph Murphy MD 6547218923471275 Thur2428
200 18.71 4.00 Male Yes Thur Lunch 3 6.24 Jason Conrad 4581233003487 Thur6048
204 20.53 4.00 Male Yes Thur Lunch 4 5.13 Scott Kim 3570611756827620 Thur2160
206 26.59 3.41 Male Yes Sat Dinner 3 8.86 Daniel Owens 38971087967574 Sat1
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
208 24.27 2.03 Male Yes Sat Dinner 2 12.14 Jason Carter 4268942915626180 Sat6048
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
211 25.89 5.16 Male Yes Sat Dinner 4 6.47 Christopher Li 6011962464150569 Sat6735
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
216 28.15 3.00 Male Yes Sat Dinner 5 5.63 Shawn Barnett PhD 4590982568244 Sat7320
217 11.59 1.50 Male Yes Sat Dinner 2 5.80 Gary Orr 30324521283406 Sat8489
218 7.74 1.44 Male Yes Sat Dinner 2 3.87 Nicholas Archer 340517153733524 Sat4772
220 12.16 2.20 Male Yes Fri Lunch 2 6.08 Ricky Johnson 213109508670736 Fri4607
222 8.58 1.92 Male Yes Fri Lunch 1 8.58 Jason Lawrence 3505302934650403 Fri6624
224 13.42 1.58 Male Yes Fri Lunch 2 6.71 Ronald Vaughn DVM 341503466406403 Fri5959
227 20.45 3.00 Male No Sat Dinner 4 5.11 Robert Bradley 213141668145910 Sat4319
228 13.28 2.72 Male No Sat Dinner 2 6.64 Glenn Jones 502061651712 Sat2937
230 24.01 2.00 Male Yes Sat Dinner 4 6.00 Michael Osborne 4258682154026 Sat7872
231 15.69 3.00 Male Yes Sat Dinner 3 5.23 Jason Parks 4812333796161 Sat6334
232 11.61 3.39 Male No Sat Dinner 2 5.80 James Taylor 6011482917327995 Sat2124
233 10.77 1.47 Male No Sat Dinner 2 5.38 Paul Novak 6011698897610858 Sat1467
234 15.53 3.00 Male Yes Sat Dinner 2 7.76 Tracy Douglas 4097938155941930 Sat7220
235 10.07 1.25 Male No Sat Dinner 2 5.04 Sean Gonzalez 3534021246117605 Sat4615
236 12.60 1.00 Male Yes Sat Dinner 2 6.30 Matthew Myers 3543676378973965 Sat5032
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
239 29.03 5.92 Male No Sat Dinner 3 9.68 Michael Avila 5296068606052842 Sat2657
241 22.67 2.00 Male Yes Sat Dinner 2 11.34 Keith Wong 6011891618747196 Sat3880
242 17.82 1.75 Male No Sat Dinner 2 8.91 Dennis Dixon 4375220550950 Sat17

157 rows × 11 columns

Multiple Conditions

Recall the steps:

  • Get the conditions
  • Wrap each condition in parenthesis
  • Use the | or & operator, depending if you want an
    • OR | (either condition is True)
    • AND & (both conditions must be True)
  • You can also use the ~ operator as a NOT operation
In [33]:
df[(df['total_bill'] > 30) & (df['sex']=='Male')]
Out[33]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
39 31.27 5.00 Male No Sat Dinner 3 10.42 Mr. Brandon Berry 6011525851069856 Sat6373
44 30.40 5.60 Male No Sun Dinner 4 7.60 Todd Cooper 503846761263 Sun2274
47 32.40 6.00 Male No Sun Dinner 4 8.10 James Barnes 3552002592874186 Sun9677
56 38.01 3.00 Male Yes Sat Dinner 4 9.50 James Christensen DDS 349793629453226 Sat8903
59 48.27 6.73 Male No Sat Dinner 4 12.07 Brian Ortiz 6596453823950595 Sat8139
83 32.68 5.00 Male Yes Thur Lunch 2 16.34 Daniel Murphy 5356177501009133 Thur8801
95 40.17 4.73 Male Yes Fri Dinner 4 10.04 Aaron Bentley 180026611638690 Fri9628
112 38.07 4.00 Male No Sun Dinner 3 12.69 Jeff Lopez 3572865915176463 Sun591
141 34.30 6.70 Male No Thur Lunch 6 5.72 Steven Carlson 3526515703718508 Thur1025
142 41.19 5.00 Male No Thur Lunch 5 8.24 Eric Andrews 4356531761046453 Thur3621
156 48.17 5.00 Male No Sun Dinner 6 8.03 Ryan Gonzales 3523151482063321 Sun7518
167 31.71 4.50 Male No Sun Dinner 4 7.93 Michael Lawson 3566285921227119 Sun3719
170 50.81 10.00 Male Yes Sat Dinner 3 16.94 Gregory Clark 5473850968388236 Sat1954
173 31.85 3.18 Male Yes Sun Dinner 2 15.92 Scott Perez 3577115550328507 Sun9335
175 32.90 3.11 Male Yes Sun Dinner 2 16.45 Nathan Reynolds 370307040837149 Sun5109
179 34.63 3.55 Male Yes Sun Dinner 2 17.32 Brian Bailey 346656312114848 Sun9851
180 34.65 3.68 Male Yes Sun Dinner 4 8.66 James Hebert DDS 676168737648 Sun7544
182 45.35 3.50 Male Yes Sun Dinner 3 15.12 Jose Parsons 4112207559459910 Sun2337
184 40.55 3.00 Male Yes Sun Dinner 2 20.27 Stephen Cox 3547798222044029 Sun5140
187 30.46 2.00 Male Yes Sun Dinner 5 6.09 David Barrett 4792882899700988 Sun9987
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
In [34]:
df[(df['total_bill'] > 30) & ~(df['sex']=='Male')]
Out[34]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
52 34.81 5.20 Female No Sun Dinner 4 8.70 Emily Daniel 4291280793094374 Sun6165
85 34.83 5.17 Female No Thur Lunch 4 8.71 Shawna Cook 6011787464177340 Thur7972
102 44.30 2.50 Female Yes Sat Dinner 3 14.77 Heather Cohen 379771118886604 Sat6240
197 43.11 5.00 Female Yes Thur Lunch 4 10.78 Brooke Soto 5544902205760175 Thur9313
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
In [35]:
df[(df['total_bill'] > 30) & (df['sex']!='Male')]
Out[35]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
52 34.81 5.20 Female No Sun Dinner 4 8.70 Emily Daniel 4291280793094374 Sun6165
85 34.83 5.17 Female No Thur Lunch 4 8.71 Shawna Cook 6011787464177340 Thur7972
102 44.30 2.50 Female Yes Sat Dinner 3 14.77 Heather Cohen 379771118886604 Sat6240
197 43.11 5.00 Female Yes Thur Lunch 4 10.78 Brooke Soto 5544902205760175 Thur9313
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
In [30]:
# The Weekend
df[(df['day'] =='Sun') | (df['day']=='Sat')]
Out[30]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
0 16.99 1.01 Female No Sun Dinner 2 8.49 Christy Cunningham 3560325168603410 Sun2959
1 10.34 1.66 Male No Sun Dinner 3 3.45 Douglas Tucker 4478071379779230 Sun4608
2 21.01 3.50 Male No Sun Dinner 3 7.00 Travis Walters 6011812112971322 Sun4458
3 23.68 3.31 Male No Sun Dinner 2 11.84 Nathaniel Harris 4676137647685994 Sun5260
4 24.59 3.61 Female No Sun Dinner 4 6.15 Tonya Carter 4832732618637221 Sun2251
5 25.29 4.71 Male No Sun Dinner 4 6.32 Erik Smith 213140353657882 Sun9679
6 8.77 2.00 Male No Sun Dinner 2 4.38 Kristopher Johnson 2223727524230344 Sun5985
7 26.88 3.12 Male No Sun Dinner 4 6.72 Robert Buck 3514785077705092 Sun8157
8 15.04 1.96 Male No Sun Dinner 2 7.52 Joseph Mcdonald 3522866365840377 Sun6820
9 14.78 3.23 Male No Sun Dinner 2 7.39 Jerome Abbott 3532124519049786 Sun3775
10 10.27 1.71 Male No Sun Dinner 2 5.14 William Riley 566287581219 Sun2546
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
12 15.42 1.57 Male No Sun Dinner 2 7.71 Chad Harrington 577040572932 Sun1300
13 18.43 3.00 Male No Sun Dinner 4 4.61 Joshua Jones 6011163105616890 Sun2971
14 14.83 3.02 Female No Sun Dinner 2 7.42 Vanessa Jones 30016702287574 Sun3848
15 21.58 3.92 Male No Sun Dinner 2 10.79 Matthew Reilly 180073029785069 Sun1878
16 10.33 1.67 Female No Sun Dinner 3 3.44 Elizabeth Foster 4240025044626033 Sun9715
17 16.29 3.71 Male No Sun Dinner 3 5.43 John Pittman 6521340257218708 Sun2998
18 16.97 3.50 Female No Sun Dinner 3 5.66 Laura Martinez 30422275171379 Sun2789
19 20.65 3.35 Male No Sat Dinner 3 6.88 Timothy Oneal 6568069240986485 Sat9213
20 17.92 4.08 Male No Sat Dinner 2 8.96 Thomas Rice 4403296224639756 Sat1709
21 20.29 2.75 Female No Sat Dinner 2 10.14 Natalie Gardner 5448125351489749 Sat9618
22 15.77 2.23 Female No Sat Dinner 2 7.88 Ashley Shelton 3524119516293213 Sat9786
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
24 19.82 3.18 Male No Sat Dinner 2 9.91 Christopher Ross 36739148167928 Sat6236
25 17.81 2.34 Male No Sat Dinner 4 4.45 Robert Perkins 30502930499388 Sat907
26 13.37 2.00 Male No Sat Dinner 2 6.68 Kyle Avery 6531339539615499 Sat6651
27 12.69 2.00 Male No Sat Dinner 2 6.34 Patrick Barber 30155551880343 Sat394
28 21.70 4.30 Male No Sat Dinner 2 10.85 David Collier 5529694315416009 Sat3697
29 19.65 3.00 Female No Sat Dinner 2 9.82 Melinda Murphy 5489272944576051 Sat2467
... ... ... ... ... ... ... ... ... ... ... ...
206 26.59 3.41 Male Yes Sat Dinner 3 8.86 Daniel Owens 38971087967574 Sat1
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
208 24.27 2.03 Male Yes Sat Dinner 2 12.14 Jason Carter 4268942915626180 Sat6048
209 12.76 2.23 Female Yes Sat Dinner 2 6.38 Sarah Cunningham 341876516331163 Sat1274
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
211 25.89 5.16 Male Yes Sat Dinner 4 6.47 Christopher Li 6011962464150569 Sat6735
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
213 13.27 2.50 Female Yes Sat Dinner 2 6.64 Robin Andersen 580140531089 Sat1374
214 28.17 6.50 Female Yes Sat Dinner 3 9.39 Marissa Jackson 4922302538691962 Sat3374
215 12.90 1.10 Female Yes Sat Dinner 2 6.45 Jessica Owen 4726904879471 Sat6983
216 28.15 3.00 Male Yes Sat Dinner 5 5.63 Shawn Barnett PhD 4590982568244 Sat7320
217 11.59 1.50 Male Yes Sat Dinner 2 5.80 Gary Orr 30324521283406 Sat8489
218 7.74 1.44 Male Yes Sat Dinner 2 3.87 Nicholas Archer 340517153733524 Sat4772
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
227 20.45 3.00 Male No Sat Dinner 4 5.11 Robert Bradley 213141668145910 Sat4319
228 13.28 2.72 Male No Sat Dinner 2 6.64 Glenn Jones 502061651712 Sat2937
229 22.12 2.88 Female Yes Sat Dinner 2 11.06 Jennifer Russell 4793003293608 Sat3943
230 24.01 2.00 Male Yes Sat Dinner 4 6.00 Michael Osborne 4258682154026 Sat7872
231 15.69 3.00 Male Yes Sat Dinner 3 5.23 Jason Parks 4812333796161 Sat6334
232 11.61 3.39 Male No Sat Dinner 2 5.80 James Taylor 6011482917327995 Sat2124
233 10.77 1.47 Male No Sat Dinner 2 5.38 Paul Novak 6011698897610858 Sat1467
234 15.53 3.00 Male Yes Sat Dinner 2 7.76 Tracy Douglas 4097938155941930 Sat7220
235 10.07 1.25 Male No Sat Dinner 2 5.04 Sean Gonzalez 3534021246117605 Sat4615
236 12.60 1.00 Male Yes Sat Dinner 2 6.30 Matthew Myers 3543676378973965 Sat5032
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
239 29.03 5.92 Male No Sat Dinner 3 9.68 Michael Avila 5296068606052842 Sat2657
240 27.18 2.00 Female Yes Sat Dinner 2 13.59 Monica Sanders 3506806155565404 Sat1766
241 22.67 2.00 Male Yes Sat Dinner 2 11.34 Keith Wong 6011891618747196 Sat3880
242 17.82 1.75 Male No Sat Dinner 2 8.91 Dennis Dixon 4375220550950 Sat17

163 rows × 11 columns

Conditional Operator isin()

We can use .isin() operator to filter by a list of options.

In [36]:
options = ['Sat','Sun']
df['day'].isin(options)
Out[36]:
0       True
1       True
2       True
3       True
4       True
5       True
6       True
7       True
8       True
9       True
10      True
11      True
12      True
13      True
14      True
15      True
16      True
17      True
18      True
19      True
20      True
21      True
22      True
23      True
24      True
25      True
26      True
27      True
28      True
29      True
       ...  
214     True
215     True
216     True
217     True
218     True
219     True
220    False
221    False
222    False
223    False
224    False
225    False
226    False
227     True
228     True
229     True
230     True
231     True
232     True
233     True
234     True
235     True
236     True
237     True
238     True
239     True
240     True
241     True
242     True
243    False
Name: day, Length: 244, dtype: bool
In [37]:
df[df['day'].isin(['Sat','Sun'])]
Out[37]:
total_bill tip sex smoker day time size price_per_person Payer Name CC Number Payment ID
0 16.99 1.01 Female No Sun Dinner 2 8.49 Christy Cunningham 3560325168603410 Sun2959
1 10.34 1.66 Male No Sun Dinner 3 3.45 Douglas Tucker 4478071379779230 Sun4608
2 21.01 3.50 Male No Sun Dinner 3 7.00 Travis Walters 6011812112971322 Sun4458
3 23.68 3.31 Male No Sun Dinner 2 11.84 Nathaniel Harris 4676137647685994 Sun5260
4 24.59 3.61 Female No Sun Dinner 4 6.15 Tonya Carter 4832732618637221 Sun2251
5 25.29 4.71 Male No Sun Dinner 4 6.32 Erik Smith 213140353657882 Sun9679
6 8.77 2.00 Male No Sun Dinner 2 4.38 Kristopher Johnson 2223727524230344 Sun5985
7 26.88 3.12 Male No Sun Dinner 4 6.72 Robert Buck 3514785077705092 Sun8157
8 15.04 1.96 Male No Sun Dinner 2 7.52 Joseph Mcdonald 3522866365840377 Sun6820
9 14.78 3.23 Male No Sun Dinner 2 7.39 Jerome Abbott 3532124519049786 Sun3775
10 10.27 1.71 Male No Sun Dinner 2 5.14 William Riley 566287581219 Sun2546
11 35.26 5.00 Female No Sun Dinner 4 8.82 Diane Macias 4577817359320969 Sun6686
12 15.42 1.57 Male No Sun Dinner 2 7.71 Chad Harrington 577040572932 Sun1300
13 18.43 3.00 Male No Sun Dinner 4 4.61 Joshua Jones 6011163105616890 Sun2971
14 14.83 3.02 Female No Sun Dinner 2 7.42 Vanessa Jones 30016702287574 Sun3848
15 21.58 3.92 Male No Sun Dinner 2 10.79 Matthew Reilly 180073029785069 Sun1878
16 10.33 1.67 Female No Sun Dinner 3 3.44 Elizabeth Foster 4240025044626033 Sun9715
17 16.29 3.71 Male No Sun Dinner 3 5.43 John Pittman 6521340257218708 Sun2998
18 16.97 3.50 Female No Sun Dinner 3 5.66 Laura Martinez 30422275171379 Sun2789
19 20.65 3.35 Male No Sat Dinner 3 6.88 Timothy Oneal 6568069240986485 Sat9213
20 17.92 4.08 Male No Sat Dinner 2 8.96 Thomas Rice 4403296224639756 Sat1709
21 20.29 2.75 Female No Sat Dinner 2 10.14 Natalie Gardner 5448125351489749 Sat9618
22 15.77 2.23 Female No Sat Dinner 2 7.88 Ashley Shelton 3524119516293213 Sat9786
23 39.42 7.58 Male No Sat Dinner 4 9.86 Lance Peterson 3542584061609808 Sat239
24 19.82 3.18 Male No Sat Dinner 2 9.91 Christopher Ross 36739148167928 Sat6236
25 17.81 2.34 Male No Sat Dinner 4 4.45 Robert Perkins 30502930499388 Sat907
26 13.37 2.00 Male No Sat Dinner 2 6.68 Kyle Avery 6531339539615499 Sat6651
27 12.69 2.00 Male No Sat Dinner 2 6.34 Patrick Barber 30155551880343 Sat394
28 21.70 4.30 Male No Sat Dinner 2 10.85 David Collier 5529694315416009 Sat3697
29 19.65 3.00 Female No Sat Dinner 2 9.82 Melinda Murphy 5489272944576051 Sat2467
... ... ... ... ... ... ... ... ... ... ... ...
206 26.59 3.41 Male Yes Sat Dinner 3 8.86 Daniel Owens 38971087967574 Sat1
207 38.73 3.00 Male Yes Sat Dinner 4 9.68 Ricky Ramirez 347817964484033 Sat4505
208 24.27 2.03 Male Yes Sat Dinner 2 12.14 Jason Carter 4268942915626180 Sat6048
209 12.76 2.23 Female Yes Sat Dinner 2 6.38 Sarah Cunningham 341876516331163 Sat1274
210 30.06 2.00 Male Yes Sat Dinner 3 10.02 Shawn Mendoza 30184049218122 Sat8361
211 25.89 5.16 Male Yes Sat Dinner 4 6.47 Christopher Li 6011962464150569 Sat6735
212 48.33 9.00 Male No Sat Dinner 4 12.08 Alex Williamson 676218815212 Sat4590
213 13.27 2.50 Female Yes Sat Dinner 2 6.64 Robin Andersen 580140531089 Sat1374
214 28.17 6.50 Female Yes Sat Dinner 3 9.39 Marissa Jackson 4922302538691962 Sat3374
215 12.90 1.10 Female Yes Sat Dinner 2 6.45 Jessica Owen 4726904879471 Sat6983
216 28.15 3.00 Male Yes Sat Dinner 5 5.63 Shawn Barnett PhD 4590982568244 Sat7320
217 11.59 1.50 Male Yes Sat Dinner 2 5.80 Gary Orr 30324521283406 Sat8489
218 7.74 1.44 Male Yes Sat Dinner 2 3.87 Nicholas Archer 340517153733524 Sat4772
219 30.14 3.09 Female Yes Sat Dinner 4 7.54 Shelby House 502097403252 Sat8863
227 20.45 3.00 Male No Sat Dinner 4 5.11 Robert Bradley 213141668145910 Sat4319
228 13.28 2.72 Male No Sat Dinner 2 6.64 Glenn Jones 502061651712 Sat2937
229 22.12 2.88 Female Yes Sat Dinner 2 11.06 Jennifer Russell 4793003293608 Sat3943
230 24.01 2.00 Male Yes Sat Dinner 4 6.00 Michael Osborne 4258682154026 Sat7872
231 15.69 3.00 Male Yes Sat Dinner 3 5.23 Jason Parks 4812333796161 Sat6334
232 11.61 3.39 Male No Sat Dinner 2 5.80 James Taylor 6011482917327995 Sat2124
233 10.77 1.47 Male No Sat Dinner 2 5.38 Paul Novak 6011698897610858 Sat1467
234 15.53 3.00 Male Yes Sat Dinner 2 7.76 Tracy Douglas 4097938155941930 Sat7220
235 10.07 1.25 Male No Sat Dinner 2 5.04 Sean Gonzalez 3534021246117605 Sat4615
236 12.60 1.00 Male Yes Sat Dinner 2 6.30 Matthew Myers 3543676378973965 Sat5032
237 32.83 1.17 Male Yes Sat Dinner 2 16.42 Thomas Brown 4284722681265508 Sat2929
238 35.83 4.67 Female No Sat Dinner 3 11.94 Kimberly Crane 676184013727 Sat9777
239 29.03 5.92 Male No Sat Dinner 3 9.68 Michael Avila 5296068606052842 Sat2657
240 27.18 2.00 Female Yes Sat Dinner 2 13.59 Monica Sanders 3506806155565404 Sat1766
241 22.67 2.00 Male Yes Sat Dinner 2 11.34 Keith Wong 6011891618747196 Sat3880
242 17.82 1.75 Male No Sat Dinner 2 8.91 Dennis Dixon 4375220550950 Sat17

163 rows × 11 columns

In [ ]:

</html>