Select rows from Pandas Dataframe Based On Column Values

In this tutorial, we are going to learn about selecting rows from Pandas Dataframe based on column values. Firstly, we need to import pandas module and create a dataframe. So, make sure that Pandas is installed in your PC. If Pandas isn’t there in your machine install it by typing the following command in Command Prompt.

pip install pandas

After installing pandas let us create a dataframe

import pandas as pd
df = pd.DataFrame(
    [['Sachin', 'India', 46, 100],
     ['Dhoni', 'India', 31, 16],
     ['Kohli', 'India', 31, 70],
     ['Kane', 'New Zealand', 29, 34],
     ['Watson', 'Australia', 38, 14],
     ['Warner', 'Australia', 33, 43],
     ['Ben Stokes', 'England', 28, 12],
     ['Kevin Pietersen', 'England', 39, 32],
     ['Dwayne Bravo', 'West Indies', 36, 5]],
    index=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    columns=['Name', 'Country', 'Age', 'Centuries']
)
print(df)

Here we have created a dataframe using Pandas Dataframe class. The output of the above code is:-

              Name      Country  Age  Centuries
0           Sachin        India   46        100
1            Dhoni        India   31         16
2            Kohli        India   31         70
3             Kane  New Zealand   29         34
4           Watson    Australia   38         14
5           Warner    Australia   33         43
6       Ben Stokes      England   28         12
7  Kevin Pietersen      England   39         32
8     Dwayne Bravo  West Indies   36          5

Accessing Based on Column Values

For example, If we want to access all the details of players from India. we can access it using loc() function which is in Pandas module. The following code snippet will print it.

print(df.loc[df['Country'] == 'India'])

The output of the above code is:-

     Name Country  Age  Centuries
0  Sachin   India   46        100
1   Dhoni   India   31         16
2   Kohli   India   31         70

As a result, the code has printed the details of Indian players. Let’s see how to access rows based on multiple column values. For this, we should create a list with required values and pass that list as an iterable for isin() function.

Example:-

x = ['Australia', 'New Zealand']
print(df.loc[df['Country'].isin(x)])

In the above code, we are printing the details of the players from Australia and New Zealand. For this, we have created a list and entered the values in it. Lastly, we have passed that list as an iterable to the isin() function.
Output:-

     Name      Country  Age  Centuries
3    Kane  New Zealand   29         34
4  Watson    Australia   38         14
5  Warner    Australia   33         43

As a result, the code provided the details of the players of Australia and New Zealand.

Also, read:- How to Normalize a Pandas DataFrame Column

Leave a Reply

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