按照字母切片,必须先对数据进行sort_index排序才能对字母进行排序
import pandas as pd
#读取college数据集
college = pd.read_csv('data/college.csv', index_col='INSTNM')
# 对college进行排序
college = college.sort_index()
college = college.head()
# 再尝试选取字母顺序在‘Sp’和‘Su’之间的学校
pd.options.display.max_rows = 6
college.loc['AB':'Al']
CITY | STABBR | HBCU | MENONLY | WOMENONLY | RELAFFIL | SATVRMID | SATMTMID | DISTANCEONLY | UGDS | ... | UGDS_2MOR | UGDS_NRA | UGDS_UNKN | PPTUG_EF | CURROPER | PCTPELL | PCTFLOAN | UG25ABV | MD_EARN_WNE_P10 | GRAD_DEBT_MDN_SUPP | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
INSTNM | |||||||||||||||||||||
ABC Beauty Academy | Garland | TX | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 30.0 | ... | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0 | 0.7857 | 0.0000 | 0.8286 | NaN | PrivacySuppressed |
ABC Beauty College Inc | Arkadelphia | AR | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 38.0 | ... | 0.0000 | 0.0000 | 0.0000 | 0.2105 | 1 | 0.9815 | 1.0000 | 0.4688 | PrivacySuppressed | 16500 |
AI Miami International University of Art and Design | Miami | FL | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 2778.0 | ... | 0.0018 | 0.0025 | 0.4644 | 0.2185 | 1 | 0.5507 | 0.6966 | 0.3262 | 29900 | 31000 |
3 rows × 26 columns
# 可以用is_monotonic_increasing或is_monotonic_decreasing检测字母排序的顺序
college = college.sort_index(ascending=False)
college.index.is_monotonic_decreasing
# 再尝试选取字母顺序在‘Sp’和‘Su’之间的学校
pd.options.display.max_rows = 6
college.loc['Al':'AB']
CITY | STABBR | HBCU | MENONLY | WOMENONLY | RELAFFIL | SATVRMID | SATMTMID | DISTANCEONLY | UGDS | ... | UGDS_2MOR | UGDS_NRA | UGDS_UNKN | PPTUG_EF | CURROPER | PCTPELL | PCTFLOAN | UG25ABV | MD_EARN_WNE_P10 | GRAD_DEBT_MDN_SUPP | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
INSTNM | |||||||||||||||||||||
AI Miami International University of Art and Design | Miami | FL | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 2778.0 | ... | 0.0018 | 0.0025 | 0.4644 | 0.2185 | 1 | 0.5507 | 0.6966 | 0.3262 | 29900 | 31000 |
ABC Beauty College Inc | Arkadelphia | AR | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 38.0 | ... | 0.0000 | 0.0000 | 0.0000 | 0.2105 | 1 | 0.9815 | 1.0000 | 0.4688 | PrivacySuppressed | 16500 |
ABC Beauty Academy | Garland | TX | 0.0 | 0.0 | 0.0 | 0 | NaN | NaN | 0.0 | 30.0 | ... | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0 | 0.7857 | 0.0000 | 0.8286 | NaN | PrivacySuppressed |
3 rows × 26 columns