Skip to main content
 首页 » 编程设计

python之使用描述获取 Pandas 中有序分类数据的最小值和最大值

2024年12月31日11bhlsheji

我有一个包含数字和分类数据的混合 Pandas 数据框。我订购了分类数据,我能够使用 min() 和 max() 函数获取最小值和最大值,但无法使用 describe 函数获取它们。有没有一种方法可以将 describe 函数与有序的分类数据一起使用并获取最小值和最大值?

代码:

data_clean.indiv1 =  
data_clean.indiv1.astype(CategoricalDtype(categories=['F', 'D', 'C', 'B', 'A'], ordered=True)) 
print('min', data_clean.indiv1.min()) 
print('max', data_clean.indiv1.max()) 
print('describe') 
print(data_clean.indiv1.describe()) 

输出:

min F 
max A 
 
# With describe() 
count     64 
unique     2 
top        A 
freq      52 
Name: indiv1, dtype: object 

请您参考如下方法:

基于document ,具有有序分类数据的 describe 函数无法获取最小值和最大值。

Using describe() on categorical data will produce similar output to a Series or DataFrame of type string.