This post is the second part of filtering a pandas DataFrame. The first part is in the post called Filtering a pandas DataFrame. In this second part I look at multiple conditions.
Let’s use a simple example by creating a pandas DataFrame manually.
2 | data1 = { 'firstname' : [ 'Bob' , 'Sally' , 'Suzie' , 'Rohan' ], |
3 | 'amount' : [ 12 , 67 , 33 , 41 ]} |
4 | df1 = pd.DataFrame(data1) |

1 | df2 = df.loc[df[ "amount" ] > = 35 ] |
The above query returns the Sally and Rohan rows.
2 | df2 = df.loc[df[ "firstname" ]. str .contains( "S" )] |
The above query returns Sally and Suzie. The contains function is case sensitive. If I had uses ‘s’ instead, it would not have returned any rows.
Using query method
3 | df3 = df.query( "firstname.str.contains('a', case=False)" ) |
The above query returns the Sally and Rohan rows.
2 | df4 = df.query( "firstname.str.contains('a', case=False) or firstname.str.contains('z')" ) |
The above returns the Sally, Suzie and Rohan rows, which is what we expected.
2 | df5 = df.query( "firstname.str.contains('a', case=False) or amount == 12" ) |
The above query returns the Bob, Sally and Rohan rows, which is what we expected. You’ll notice that the quotes need to be different.
2 | df6 = df.query( "firstname.str.contains('a', case=False) and amount == 67" ) |
The above query returns the Sally row.