在本教程中,我們將學習用戶如何使用 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-hant/n/291168.html