比较 pandas.DataFrame 中列之间的分类变量

Comparing categorical variables between columns in pandas.DataFrame

如何使用设置的分类规则而不是词典顺序规则进行比较?

给定数据集:

df = pd.DataFrame({
    'NUMBER':[12, 26, 16, 34, 38, 1, 26, 8],
    'SHIRT_SIZE':['S', 'M', 'XL', 'L', 'S', 'M', 'L', 'XL'],
    'SHIRT_SIZE2':['M', 'S', 'L', 'XL', 'M', 'L', 'XL', 'S']
})
from pandas.api.types import CategoricalDtype
c_dtype = CategoricalDtype(categories = ["S","M","L","XL"],ordered = True)
df['SHIRT_SIZE'] = df['SHIRT_SIZE'].astype(c_dtype)
df['SHIRT_SIZE2'] = df['SHIRT_SIZE2'].astype(c_dtype)
NUMBER SHIRT_SIZE SHIRT_SIZE2
0 12 S M
1 26 M S
2 16 XL L
3 34 L XL
4 38 S M
5 1 M L
6 26 L XL
7 8 XL S

'SHIRT_SIZE''SHIRT_SIZE2'dtypeCategories (4, object): ['S' < 'M' < 'L' < 'XL'] 我想比较两列 'SHIRT_SIZE''SHIRT_SIZE2'

之间的衬衫尺码

我尝试过:

def compare_size(row):
    if (row['SHIRT_SIZE'] < row['SHIRT_SIZE2']):
        return 'SMALLER'
    elif (row['SHIRT_SIZE'] > row['SHIRT_SIZE2']):
        return 'LARGER'
    else:
        return 'SAME'

df['COMPARE_SIZE'] = df.apply(lambda row: compare_size(row), axis=1)

导致:

NUMBER SHIRT_SIZE SHIRT_SIZE2 COMPARE_SIZE
0 12 S M LARGER
1 26 M S SMALLER
2 16 XL L LARGER
3 34 L XL SMALLER
4 38 S M LARGER
5 1 M L LARGER
6 26 L XL SMALLER
7 8 XL S LARGER

请注意,有一些行 例如第 0 行 'S' -> 'M' 和第 1 行 'M' -> 'S' 不遵循我们的分类 dtype 规则的顺序

从逻辑上讲,解释是:“SHIRT_SIZE 比 SHIRT_SIZE2”

我猜测字符串的词典顺序是用于比较这些衬衫尺码的基本规则,而不是我们在 Categories (4, object): ['S' < 'M' < 'L' < 'XL'].

中设置的分类规则

我希望按照分类顺序比较衬衫尺码。

使用 numpy select 比较值并生成新列:

condlist = [df.SHIRT_SIZE.gt(df.SHIRT_SIZE2), df.SHIRT_SIZE.lt(df.SHIRT_SIZE2)]
result_list = ["LARGER", "SMALLER"]
compare_size = np.select(condlist, result_list, "SAME")
df.assign(compare_size=compare_size)


    NUMBER  SHIRT_SIZE  SHIRT_SIZE2     compare_size
0   12  S   M   SMALLER
1   26  M   S   LARGER
2   16  XL  L   LARGER
3   34  L   XL  SMALLER
4   38  S   M   SMALLER
5   1   M   L   SMALLER
6   26  L   XL  SMALLER
7   8   XL  S   LARGER