Python Pandas - 从有序类别索引中获取最小值


要从有序类别索引中获取最小值,请在 Pandas 中使用 **catIndex.min()** 方法。首先,导入所需的库 -

import pandas as pd

使用“categories”参数为类别设置类别。使用“ordered”参数将类别视为有序 -

catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

显示类别索引 -

print("Categorical Index...\n",catIndex)

获取最小值 -

print("\nMinimum value from CategoricalIndex...\n",catIndex.min())

范例

以下是代码 -

import pandas as pd

# CategoricalIndex is the Index based on an underlying Categorical
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

# Display the Categorical Index
print("Categorical Index...\n",catIndex)

# Get the categories
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)

# Get the min value
print("\nMinimum value from CategoricalIndex...\n",catIndex.min())

输出

将产生以下输出 -

Categorical Index...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')

DisplayingCategories from CategoricalIndex...
Index(['p', 'q', 'r', 's'], dtype='object')

Minimum value from CategoricalIndex...
P

更新于:14-Oct-2021

96 次观看

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.