Python Pandas - 分類資料比較



比較分類資料是獲得見解和理解資料不同類別之間關係的一項基本任務。在 Python 中,Pandas 提供了多種方法來使用比較運算子(==、!=、>、>=、< 和 <=)對分類資料執行比較。這些比較可以在三種主要場景中進行:

  • 相等比較(== 和 !=)。

  • 所有比較(==、!=、>、>=、< 和 <=)。

  • 將分類資料與標量值進行比較。

需要注意的是,在具有不同類別的分類資料之間或在分類 Series 與列表狀物件之間進行任何非相等比較都會引發TypeError。這是因為類別的順序可以解釋為兩種方式,一種考慮順序,另一種不考慮順序。

在本教程中,我們將學習如何在 Python Pandas 庫中使用比較運算子(如==!=>>=<<=)來比較分類資料。

分類資料的相等比較

在 Pandas 中,可以使用各種物件(如列表、陣列或與分類資料長度相同的 Series 物件)對分類資料進行相等比較。

示例

以下示例演示瞭如何在分類 Series 和列表狀物件之間執行相等和不等比較。

import pandas as pd
from pandas.api.types import CategoricalDtype
import numpy as np

# Creating a categorical Series
s = pd.Series([1, 2, 1, 1, 2, 3, 1, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

# Creating another categorical Series for comparison
s2 = pd.Series([2, 2, 2, 1, 1, 3, 3, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

# Equality comparison
print("Equality comparison (s == s2):")
print(s == s2)

print("\nInequality comparison (s != s2):")
print(s != s2)

# Equality comparison with a NumPy array
print("\nEquality comparison with NumPy array:")
print(s == np.array([1, 2, 3, 1, 2, 3, 2, 1]))

以下是上述程式碼的輸出:

Equality comparison (s == s2):
0    False
1     True
2    False
3     True
4    False
5     True
6    False
7     True
dtype: bool

Inequality comparison (s != s2):
0     True
1    False
2     True
3    False
4     True
5    False
6     True
7    False
dtype: bool

Equality comparison with NumPy array:
0     True
1     True
2    False
3     True
4     True
5     True
6    False
7    False
dtype: bool

分類資料的所有比較

Pandas 允許您在有序分類資料之間執行各種比較操作,包括(>、>=、<=、<)。

示例

此示例演示瞭如何在有序分類資料上執行非相等比較(>、>=、<=、<)。

import pandas as pd
from pandas.api.types import CategoricalDtype
import numpy as np

# Creating a categorical Series
s = pd.Series([1, 2, 1, 1, 2, 3, 1, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

# Creating another categorical Series for comparison
s2 = pd.Series([2, 2, 2, 1, 1, 3, 3, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

# Greater than comparison 
print("Greater than comparison:\n",s > s2)

# Less than comparison 
print("\nLess than comparison:\n",s < s2)

# Greater than or equal to comparison 
print("\nGreater than or equal to comparison:\n",s >= s2)

# Lessthan or equal to comparison 
print("\nLess than or equal to comparison:\n",s <= s2)

以下是上述程式碼的輸出:

Greater than comparison: 
0     True
1    False
2     True
3    False
4    False
5    False
6     True
7    False
dtype: bool

Less than comparison: 
0    False
1    False
2    False
3    False
4     True
5    False
6    False
7    False
dtype: bool

Greater than or equal to comparison: 
0     True
1     True
2     True
3     True
4    False
5     True
6     True
7     True
dtype: bool

Lessthan or equal to comparison: 
0    False
1     True
2    False
3     True
4     True
5     True
6    False
7     True
dtype: bool

將分類資料與標量進行比較

分類資料也可以使用所有比較運算子(==、!=、>、>=、< 和 <=)與標量值進行比較。分類值根據其類別的順序與標量進行比較。

示例

以下示例演示瞭如何將分類資料與標量值進行比較。

import pandas as pd

# Creating a categorical Series
s = pd.Series([1, 2, 3]).astype(pd.CategoricalDtype([3, 2, 1], ordered=True))

# Compare to a scalar
print("Comparing categorical data to a scalar:")
print(s > 2)

以下是上述程式碼的輸出:

Comparing categorical data to a scalar:
0     True
1    False
2    False
dtype: bool

比較具有不同類別的分類資料

當比較兩個具有不同類別或排序的分類 Series 時,將引發TypeError

示例

以下示例演示了在兩個具有不同類別或順序的分類 Series 物件之間執行比較時如何處理TypeError

import pandas as pd
from pandas.api.types import CategoricalDtype
import numpy as np

# Creating a categorical Series
s = pd.Series([1, 2, 1, 1, 2, 3, 1, 3]).astype(CategoricalDtype([3, 2, 1], ordered=True))

# Creating another categorical Series for comparison
s3 = pd.Series([2, 2, 2, 1, 1, 3, 1, 2]).astype(CategoricalDtype(ordered=True))

try:
    print("Attempting to compare differently ordered two Series objects:")
    print(s > s3)
except TypeError as e:
    print("TypeError:", str(e))

以下是上述程式碼的輸出:

Attempting to compare differently ordered two Series objects:
TypeError: Categoricals can only be compared if 'categories' are the same.
廣告