Javatpoint Logo
Javatpoint Logo

How to Select rows in Pandas DataFrame Based on Conditions

In this tutorial, we will learn how a user can select rows in Pandas DataFrame based on conditions using Python.

Users can select rows based on a particular column value using '>', '=', '<=', '>=', '!=' operators.

Conditions:

We will discuss different conditions that can be applied to the Pandas DataFrame.

Condition 1:

Select all the rows from the DataFrame in which 'Percentage' is greater than 70 using the basic method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
5    John     24          ADS            78
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Condition 2:

Select all the rows from the DataFrame in which 'Percentage' is greater than 70 by using the "loc[]" method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
5    John     24          ADS            78
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Condition 3:

Select all the rows from the DataFrame in which 'Percentage' is not equal to 71 using the "loc[]" method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
    Name_1  Age_1 Subjects_1  Percentage_1
0    Anuj     23       DBMS            88
1    Ashu     24        ADS            62
2   Yashi     21       ASPM            85
4  Joshua     21       MFCS            55
5    John     24        ADS            78
6     Ray     25       ASPM            70
7   Lilly     22        TOC            66
9  Rachel     22       OOPS            89

Now, we will learn how to select those rows whose column value is present in the list by using the "isin()" function of the DataFrame.

Condition 4:

Select all the rows from the given DataFrame in which column value of "Subjects_1" is present in the "Subjects_2" list by using the basic method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
   Name_1  Age_1 Subjects_1  Percentage_1
1   Ashu     24        ADS            62
2  Yashi     21       ASPM            85
5   John     24        ADS            78
6    Ray     25       ASPM            70
7  Lilly     22        TOC            66

Condition 5:

Select all the rows from the given DataFrame in which column value of "Subjects_1" is present in the "Subjects_2" list by using the "loc[]" method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
   Name_1  Age_1 Subjects_1  Percentage_1
1   Ashu     24        ADS            62
2  Yashi     21       ASPM            85
5   John     24        ADS            78
6    Ray     25       ASPM            70
7  Lilly     22        TOC            66

Condition 6:

Select all the rows from the given DataFrame in which column value of "Subjects_1" is not present in the "Subjects_2" list by using the "loc[]" method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     24          ADS            62
2   Yashi     21         ASPM            85
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
3    Mark     19          BCM            71
4  Joshua     21         MFCS            55
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Now, we will learn how to select rows based on multiple column conditions by using the "&" operator.

Condition 7:

Select all the rows from the given DataFrame in which "Percentage_1" is equal to "71" and "Subject_1" is present in the "Subject_2" list by using the basic method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     21          ADS            71
2   Yashi     21         ASPM            71
3    Mark     19          BCM            82
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
   Name_1  Age_1 Subjects_1  Percentage_1
1   Ashu     21        ADS            71
2  Yashi     21       ASPM            71

Condition 8:

Select all the rows from the given DataFrame in which "Percentage_1" is equal to "71" and "Subject_1" is present in the "Subject_2" list by using the "loc[]" method.

Code:

Output:

Given DataFrame: 
    Name_1  Age_1   Subjects_1  Percentage_1
0    Anuj     23         DBMS            88
1    Ashu     21          ADS            71
2   Yashi     21         ASPM            71
3    Mark     19          BCM            82
4  Joshua     21         MFCS            55
5    John     24          ADS            78
6     Ray     25         ASPM            70
7   Lilly     22          TOC            66
8    Rose     23  Data Mining            71
9  Rachel     22         OOPS            89

Following is the Result DataFrame: 
   Name_1  Age_1 Subjects_1  Percentage_1
1   Ashu     21        ADS            71
2  Yashi     21       ASPM            71

Conclusion

In this tutorial, we have discussed how to select different rows of Pandas DataFrame based on various conditions.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA