Python Pandas – 从有序分类型索引中获取最大值
要从有序分类索引中获取最大值,请在Pandas中使用catIndex.max()方法。
首先,导入所需的库−
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("分类索引...\n",catIndex)
获取最大值−
print("\n从CategoricalIndex获取最大值...\n",catIndex.max())
更多Pandas相关文章,请阅读:Pandas 教程
示例
以下是代码−
import pandas as pd
# CategoricalIndex只能使用有限的、通常是固定的可能值。
# 使用“categories”参数设置分类的类别
# 使用“ordered”参数将分类视为有序
catIndex = pd.CategoricalIndex(
["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)
# 显示目录索引
print("分类索引...\n",catIndex)
# 获取类别
print("\n从CategoricalIndex中显示类别...\n",catIndex.categories)
# 获取最大值
print("\n从CategoricalIndex获取最大值...\n",catIndex.max())
输出
这将产生以下输出−
分类索引...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')
从CategoricalIndex中显示类别...
Index(['p', 'q', 'r', 's'], dtype='object')
从CategoricalIndex获取最大值...
S
极客教程