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.