如何根據條件選擇 Pandas 數據幀中的行

在本教程中,我們將學慣用戶如何使用 Python 基於條件選擇 Pandas 數據幀中的行。

用戶可以使用「>」、「=」、「< =」、「> =」、「!= ‘運算符。

條件:

我們將討論適用於 Pandas 數據幀的不同條件。

條件 1:

使用基本方法從數據幀中選擇「百分比」大於 70 的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subject_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

# Then we will select rows based on condition
result_DataFrame = Data_Frame[Data_Frame['Percentage_1'] > 70]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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[] 」方法從數據幀中選擇「百分比」大於 70 的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

# Then we will select rows based on condition, That is, Using loc[] method
result_DataFrame = Data_Frame.loc[Data_Frame['Percentage_1'] > 70]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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[] 」方法從數據幀中選擇「百分比」不等於 71 的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

# Then we will select rows based on condition, That is, Using loc[] method
result_DataFrame = Data_Frame.loc[Data_Frame['Percentage_1'] != 71]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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:

使用基本方法,選擇給定數據幀中「科目 _1 」列值出現在「科目 _2 列表中的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

Subjects_2 = ['ASPM', 'ADS', 'TOC']

# Then we will select rows based on condition, That is, Using isin[] method
result_DataFrame = Data_Frame[Data_Frame['Subjects_1'].isin(Subjects_2)]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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[] 」方法選擇給定數據幀中「主題 _1 」列值出現在「主題 _2 」列表中的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

Subjects_2 = ['ASPM', 'ADS', 'TOC']

# Then we will select rows based on condition, That is, Using isin[] method
result_DataFrame = Data_Frame.loc[Data_Frame['Subjects_1'].isin(Subjects_2)]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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[] 」方法選擇給定數據幀中「主題 _1 」列值不在「主題 _2 」列表中的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 24, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 62, 85, 71, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

Subjects_2 = ['ASPM', 'ADS', 'TOC']

# Then we will select rows based on condition, That is, Using isin[] method
result_DataFrame = Data_Frame.loc[~Data_Frame['Subjects_1'].isin(Subjects_2)]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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:

使用基本方法,從給定數據幀中選擇「百分比 _1 」等於「 71 」且「主題 _1 」出現在「主題 _2 列表中的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 21, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 71, 71, 82, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

Subjects_2 = ['ASPM', 'ADS', 'TOC']

# Then we will select rows based on condition, That is, Using isin[] method
result_DataFrame = Data_Frame[(Data_Frame['Percentage_1'] == 71) &
                              Data_Frame['Subjects_1'].isin(Subjects_2)]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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【】」方法選擇給定數據幀中「百分比 _1」等於「 71 」且「主題 _1 」出現在「主題 _2 」列表中的所有行。

代碼:


# First, import pandas
import pandas as pnd
record_1 = {

 'Name_1': ['Anuj', 'Ashu', 'Yashi', 'Mark', 'Joshua', 'John', 'Ray', 'Lilly', 'Rose', 'Rachel' ],
 'Age_1': [23, 21, 21, 19, 21, 24, 25, 22, 23, 22],
 'Subjects_1': ['DBMS', 'ADS', 'ASPM', 'BCM', 'MFCS', 'ADS', 'ASPM', 'TOC', 'Data Mining', 'OOPS'],
 'Percentage_1': [88, 71, 71, 82, 55, 78, 70, 66, 71, 89] }

# Now, we are creating a dataframe
Data_Frame = pnd.DataFrame(record_1, columns = ['Name_1', 'Age_1', 'Subjects_1', 'Percentage_1'])

print("Given DataFrame: \n", Data_Frame) 

Subjects_2 = ['ASPM', 'ADS', 'TOC']

# Then we will select rows based on condition, That is, Using isin[] method
result_DataFrame = Data_Frame.loc[(Data_Frame['Percentage_1'] == 71) &
                              Data_Frame['Subjects_1'].isin(Subjects_2)]

print('\nFollowing is the Result DataFrame: \n', result_DataFrame)

輸出:

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 數據幀的不同行。


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291168.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-24 13:14
下一篇 2024-12-24 13:14

相關推薦

  • Python讀取CSV數據畫散點圖

    本文將從以下方面詳細闡述Python讀取CSV文件並畫出散點圖的方法: 一、CSV文件介紹 CSV(Comma-Separated Values)即逗號分隔值,是一種存儲表格數據的…

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • 如何用Python統計列表中各數據的方差和標準差

    本文將從多個方面闡述如何使用Python統計列表中各數據的方差和標準差, 並給出詳細的代碼示例。 一、什麼是方差和標準差 方差是衡量數據變異程度的統計指標,它是每個數據值和該數據值…

    編程 2025-04-29
  • Python多線程讀取數據

    本文將詳細介紹多線程讀取數據在Python中的實現方法以及相關知識點。 一、線程和多線程 線程是操作系統調度的最小單位。單線程程序只有一個線程,按照程序從上到下的順序逐行執行。而多…

    編程 2025-04-29
  • Python兩張表數據匹配

    本篇文章將詳細闡述如何使用Python將兩張表格中的數據匹配。以下是具體的解決方法。 一、數據匹配的概念 在生活和工作中,我們常常需要對多組數據進行比對和匹配。在數據量較小的情況下…

    編程 2025-04-29
  • Python爬取公交數據

    本文將從以下幾個方面詳細闡述python爬取公交數據的方法: 一、準備工作 1、安裝相關庫 import requests from bs4 import BeautifulSou…

    編程 2025-04-29
  • Python數據標準差標準化

    本文將為大家詳細講述Python中的數據標準差標準化,以及涉及到的相關知識。 一、什麼是數據標準差標準化 數據標準差標準化是數據處理中的一種方法,通過對數據進行標準差標準化可以將不…

    編程 2025-04-29
  • 如何使用Python讀取CSV數據

    在數據分析、數據挖掘和機器學習等領域,CSV文件是一種非常常見的文件格式。Python作為一種廣泛使用的編程語言,也提供了方便易用的CSV讀取庫。本文將介紹如何使用Python讀取…

    編程 2025-04-29
  • Python如何打亂數據集

    本文將從多個方面詳細闡述Python打亂數據集的方法。 一、shuffle函數原理 shuffle函數是Python中的一個內置函數,主要作用是將一個可迭代對象的元素隨機排序。 在…

    編程 2025-04-29
  • Python根據表格數據生成折線圖

    本文將介紹如何使用Python根據表格數據生成折線圖。折線圖是一種常見的數據可視化圖表形式,可以用來展示數據的趨勢和變化。Python是一種流行的編程語言,其強大的數據分析和可視化…

    編程 2025-04-29

發表回復

登錄後才能評論