Pandas Loc Multiple Conditions

Select rows by multiple conditions using loc in Pandas

💡 Outline

Here is the code to select rows by pandas Loc multiple conditions.

 import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])

print(df.loc[(df['Age']>18)&(df['Name']=='Jay')])

Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay.

     Name Gender  Age
0     Jay      M   21

The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels.

We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a condition with the DataFrame.

See the code below.

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
print(df['Age']>18)    

Output:

0     True
1    False
2     True
3    False
Name: Age, dtype: bool

Note that this returns a Series object with True or False values depending on the rows. We can use this object to extract the rows which satisfy the condition.

For example,

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
print(df[df['Age']>18])    

Output:

     Name Gender  Age
0     Jay      M   21
2  Preity      F   19

In the above example, we extracted the rows from a DataFrame where age was greater than 18. Similarly, we can specify multiple conditions.

While giving multiple conditions, remember that we need to separate the conditions using the relational operators.

  • We can use the OR operator when we want to print the rows if at least even one condition is True.
  • The AND operator is used when we wish to return rows where both the conditions are True.

We can use the loc() function also to extract rows based on some condition. We will repeat what we did in the previous example using the loc() function.

See the code below.

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
print(df.loc[(df['Age']>18)])    

Output:

     Name Gender  Age
0     Jay      M   21
2  Preity      F   19

This method is a lot cleaner. Now we will specify multiple conditions within the loc() function.

For example,

import pandas as pd
df = pd.DataFrame([['Jay','M',21],['Jennifer','F',17],
                   ['Preity','F',19],['Neil','M',17]], 
                  columns = ['Name','Gender','Age'])
print(df.loc[(df['Age']>18)&(df['Name']=='Jay')])

Output:

     Name Gender  Age
0     Jay      M   21

In the above example, we extracted the rows from the DataFrame, where the Name was equal to Jay and Age was greater than 18. Both the conditions were separated using the & operator. This required both the conditions to be true in any given row.

That’s all about how to Select rows with Pandas loc multiple conditions.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.