简体   繁体   English

python 中不区分大小写的排序

[英]Case insensitive sorting in python

I have a dataframe with X rows and X columns.我有一个带有 X 行和 X 列的 dataframe。 I would like to display only a part of the table which is sorted (case-insensitive).我只想显示已排序的表格的一部分(不区分大小写)。

Ie, if I extract only a particular column with their count based on the unique rows say df.ROI.values_count() I get an output that looks like the following:即,如果我根据唯一行说df.ROI.values_count()仅提取特定列及其计数,我会得到一个 output ,如下所示:

ABDOMINAL CAVITY 48445
ADRENAL GLAND    18889
Abdominal cavity 18282
Accessory sinus  12088
Adipose tissue    3048
Adrenal gland        5
Name: ROI, dtype: int64

But, I would like my output to be sorted case-insensitively.但是,我希望对我的 output 进行不区分大小写的排序。 Like the following:如下所示:

ABDOMINAL CAVITY 48445
Abdominal cavity 18282
ADRENAL GLAND    18889
Adrenal gland        5
Accessory sinus  12088
Adipose tissue    3048
Name: ROI, dtype: int64

How do I do that?我怎么做?

Use Series.sort_index with parameter key :使用带有参数keySeries.sort_index

s = df.ROI.values_count()

s = s.sort_index(key=lambda x: x.str.lower())
print (s)
ABDOMINAL CAVITY    48445
Abdominal cavity    18282
Accessory sinus     12088
Adipose tissue       3048
ADRENAL GLAND       18889
Adrenal gland           5
Name: ROI, dtype: int64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM