如何在 Pandas DataFrame 中基于条件选择行

2024年8月29日 | 阅读 8 分钟

在本教程中,我们将学习用户如何使用 Python 在 Pandas DataFrame 中根据条件选择行。

用户可以使用 '>', '=', '<=', '>=', '!=' 运算符根据特定列值选择行。

条件

我们将讨论可以应用于 Pandas DataFrame 的不同条件。

条件 1

使用基本方法选择 DataFrame 中“Percentage”大于 70 的所有行。

代码

输出

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

条件 2

使用“loc[]”方法选择 DataFrame 中“Percentage”大于 70 的所有行。

代码

输出

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

条件 3

使用“loc[]”方法选择 DataFrame 中“Percentage”不等于 71 的所有行。

代码

输出

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

现在,我们将学习如何使用 DataFrame 的 "isin()" 函数选择列值存在于列表中的行。

条件 4

使用基本方法选择给定 DataFrame 中“Subjects_1”的列值存在于“Subjects_2”列表中的所有行。

代码

输出

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

条件 5

使用“loc[]”方法选择给定 DataFrame 中“Subjects_1”的列值存在于“Subjects_2”列表中的所有行。

代码

输出

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

条件 6

使用“loc[]”方法选择给定 DataFrame 中“Subjects_1”的列值不存在于“Subjects_2”列表中的所有行。

代码

输出

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

现在,我们将学习如何使用 "&" 运算符根据多个列条件选择行。

条件 7

使用基本方法选择给定 DataFrame 中“Percentage_1”等于“71”且“Subject_1”存在于“Subject_2”列表中的所有行。

代码

输出

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

条件 8

使用“loc[]”方法选择给定 DataFrame 中“Percentage_1”等于“71”且“Subject_1”存在于“Subject_2”列表中的所有行。

代码

输出

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

结论

在本教程中,我们讨论了如何根据各种条件选择 Pandas DataFrame 的不同行。