Python Pandas - 二元比較運算
Pandas 中的二元比較運算用於比較 Pandas 資料結構(例如 Series 或 DataFrame 物件)中的元素與標量值或其他資料結構。這些運算返回布林結果,指示每個比較的結果,這些運算對於過濾、基於條件的運算和資料分析非常有用。
在本教程中,您將學習如何在 Pandas 資料結構上對標量值和其他 DataFrame/Series 物件執行小於、大於、等於等二元比較運算。
Pandas 中的二元比較運算子
二元比較運算子用於比較 Pandas Series 或 DataFrame 中的元素與標量值。這些運算的結果是一個布林資料結構,其中 True 表示滿足給定條件,False 表示不滿足。
以下是可用於 Pandas DataFrame 或 Series 的常用二元比較運算子列表:
<:檢查每個元素是否小於給定值。
>:檢查每個元素是否大於給定值。
<=:檢查每個元素是否小於或等於給定值。
>=:檢查每個元素是否大於或等於給定值。
==:檢查每個元素是否等於給定值。
!=:檢查每個元素是否不等於給定值。
示例
以下示例演示如何將比較運算子應用於帶有標量值的 Pandas DataFrame。
import pandas as pd
# Create a sample DataFrame
data = {'A': [1, 5, 3, 8], 'B': [4, 6, 2, 9]}
df = pd.DataFrame(data)
# Display the input DataFrame
print("Input DataFrame:\n", df)
# Perform binary comparison operations
print("\nLess than 5:\n", df < 5)
print("\nGreater than 5:\n", df > 5)
print("\nLess than or equal to 5:\n", df <= 5)
print("\nGreater than or equal to 5:\n", df >= 5)
print("\nEqual to 5:\n", df == 5)
print("\nNot equal to 5:\n", df != 5)
以下是上述程式碼的輸出:
Input DataFrame:
Less than 5:
| A | B |
0 | True | True |
1 | False | False |
2 | True | True |
3 | False | False |
Greater than 5:
| A | B |
0 | False | False |
1 | False | True |
2 | False | False |
3 | True | True |
Less than or equal to 5:
| A | B |
0 | True | True |
1 | True | False |
2 | True | True |
3 | False | False |
Greater than or equal to 5:
| A | B |
0 | False | False |
1 | True | True |
2 | False | False |
3 | True | True |
Equal to 5:
| A | B |
0 | False | False |
1 | True | False |
2 | False | False |
3 | False | False |
Not equal to 5:
| A | B |
0 | True | True |
1 | False | True |
2 | True | True |
3 | True | True |
Pandas 中的二元比較函式
除了上述運算子外,Pandas 還提供各種函式來對 Pandas 資料結構執行二元比較運算,並提供其他自定義選項,例如選擇軸和為多級索引物件指定級別。
以下是 Pandas 中二元比較函式的列表:
序號 |
函式 |
描述 |
1 |
lt(other[, axis, level]) |
逐元素小於比較。 |
2 |
gt(other[, axis, level]) |
逐元素大於比較。 |
3 |
le(other[, axis, level]) |
逐元素小於或等於比較。 |
4 |
ge(other[, axis, level]) |
逐元素大於或等於比較。 |
5 |
ne(other[, axis, level]) |
逐元素不相等比較。 |
6 |
eq(other[, axis, level]) |
逐元素相等比較。 |
示例:Pandas Series 上的二元比較運算
此示例演示了 Pandas Series 和標量值之間應用二元比較函式。
import pandas as pd
# Create a Pandas Series
s = pd.Series([10, 20, 30, 40, 50])
# Display the Series
print("Pandas Series:\n", s)
# Perform comparison operations
print("\nLess than 25:\n", s.lt(25))
print("\nGreater than 25:\n", s.gt(25))
print("\nLess than or equal to 30:\n", s.le(30))
print("\nGreater than or equal to 40:\n", s.ge(40))
print("\nNot equal to 30:\n", s.ne(30))
print("\nEqual to 50:\n", s.eq(50))
以下是上述程式碼的輸出:
Pandas Series:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Less than 25:
0 True
1 True
2 False
3 False
4 False
dtype: bool
Greater than 25:
0 False
1 False
2 True
3 True
4 True
dtype: bool
Less than or equal to 30:
0 True
1 True
2 True
3 False
4 False
dtype: bool
Greater than or equal to 40:
0 False
1 False
2 False
3 True
4 True
dtype: bool
Not equal to 30:
0 True
1 True
2 False
3 True
4 True
dtype: bool
Equal to 50:
0 False
1 False
2 False
3 False
4 True
dtype: bool
示例:Pandas DataFrame 上的二元比較運算
與上述示例類似,這將使用 Pandas 中的二元比較函式在 DataFrame 和標量值之間執行二元比較運算。
import pandas as pd
# Create a DataFrame
data = {'A': [10, 20, 30], 'B': [40, 50, 60]}
df = pd.DataFrame(data)
# Display the DataFrame
print("DataFrame:\n", df)
# Perform comparison operations
print("\nLess than 25:\n", df.lt(25))
print("\nGreater than 50:\n", df.gt(50))
print("\nEqual to 30:\n", df.eq(30))
print("\nLess than or equal to 30:\n", df.le(30))
print("\nGreater than or equal to 40:\n", df.ge(40))
print("\nNot equal to 30:\n", df.ne(30))
以下是上述程式碼的輸出:
DataFrame:
Less than 25:
| A | B |
0 | True | False |
1 | True | False |
2 | False | False |
Greater than 50:
| A | B |
0 | False | False |
1 | False | False |
2 | False | True |
Equal to 30:
| A | B |
0 | False | False |
1 | False | False |
2 | True | False |
Less than or equal to 30:
| A | B |
0 | True | False |
1 | True | False |
2 | True | False |
Greater than or equal to 40:
| A | B |
0 | False | True |
1 | False | True |
2 | False | True |
Not equal to 30:
| A | B |
0 | True | True |
1 | True | True |
2 | False | True |
示例:兩個 Pandas 資料結構之間的二元比較
此示例使用 eq()、ne()、lt()、gt()、le() 和 gt() 函式逐元素比較兩個 DataFrame。
import pandas as pd
# Create two DataFrames
df1 = pd.DataFrame({'A': [1, 0, 3], 'B': [9, 5, 6]})
df2 = pd.DataFrame({'A': [1, 2, 1], 'B': [6, 5, 4]})
# Display the Input DataFrames
print("DataFrame 1:\n", df1)
print("\nDataFrame 2:\n", df2)
# Perform comparison operations between two DataFrames
print("\nEqual :\n", df1.eq(df2))
print("\nNot Equal:\n", df1.ne(df2))
print("\ndf1 Less than df2:\n", df1.lt(df2))
print("\ndf1 Greater than df2:\n", df1.gt(df2))
print("\ndf1 Less than or equal to df2:\n", df1.le(df2))
print("\ndf1 Greater than or equal to df2:\n", df1.ge(df2))
以下是上述程式碼的輸出:
DataFrame 1:
DataFrame 2:
Equal :
|
A |
B |
0 | True | False |
1 | False | True |
2 | False | False |
Not Equal:
|
A |
B |
0 | False | True |
1 | True | False |
2 | True | True |
df1 Less than df2:
|
A |
B |
0 | False | False |
1 | True | False |
2 | False | False |
df1 Greater than df2:
|
A |
B |
0 | False | True |
1 | False | False |
2 | True | True |
df1 Less than or equal to df2:
| A | B |
0 | True | False |
1 | True | True |
2 | False | False |
df1 Greater than or equal to df2:
| A | B |
0 | True | True |
1 | False | True |
2 | True | True |