7. 按照字母切片

  1. # 读取college数据集;尝试选取字母顺序在‘Sp’和‘Su’之间的学校
  2. In[57]: college = pd.read_csv('data/college.csv', index_col='INSTNM')
  3. college.loc['Sp':'Su']
  4. ---------------------------------------------------------------------------
  5. ValueError Traceback (most recent call last)
  6. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind)
  7. 3483 try:
  8. -> 3484 return self._searchsorted_monotonic(label, side)
  9. 3485 except ValueError:
  10. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in _searchsorted_monotonic(self, label, side)
  11. 3442
  12. -> 3443 raise ValueError('index must be monotonic increasing or decreasing')
  13. 3444
  14. ValueError: index must be monotonic increasing or decreasing
  15. During handling of the above exception, another exception occurred:
  16. KeyError Traceback (most recent call last)
  17. <ipython-input-57-c9f1c69a918b> in <module>()
  18. 1 college = pd.read_csv('data/college.csv', index_col='INSTNM')
  19. ----> 2 college.loc['Sp':'Su']
  20. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
  21. 1326 else:
  22. 1327 key = com._apply_if_callable(key, self.obj)
  23. -> 1328 return self._getitem_axis(key, axis=0)
  24. 1329
  25. 1330 def _is_scalar_access(self, key):
  26. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
  27. 1504 if isinstance(key, slice):
  28. 1505 self._has_valid_type(key, axis)
  29. -> 1506 return self._get_slice_axis(key, axis=axis)
  30. 1507 elif is_bool_indexer(key):
  31. 1508 return self._getbool_axis(key, axis=axis)
  32. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexing.py in _get_slice_axis(self, slice_obj, axis)
  33. 1354 labels = obj._get_axis(axis)
  34. 1355 indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop,
  35. -> 1356 slice_obj.step, kind=self.name)
  36. 1357
  37. 1358 if isinstance(indexer, slice):
  38. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in slice_indexer(self, start, end, step, kind)
  39. 3348 """
  40. 3349 start_slice, end_slice = self.slice_locs(start, end, step=step,
  41. -> 3350 kind=kind)
  42. 3351
  43. 3352 # return a slice
  44. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in slice_locs(self, start, end, step, kind)
  45. 3536 start_slice = None
  46. 3537 if start is not None:
  47. -> 3538 start_slice = self.get_slice_bound(start, 'left', kind)
  48. 3539 if start_slice is None:
  49. 3540 start_slice = 0
  50. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind)
  51. 3485 except ValueError:
  52. 3486 # raise the original KeyError
  53. -> 3487 raise err
  54. 3488
  55. 3489 if isinstance(slc, np.ndarray):
  56. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind)
  57. 3479 # we need to look up the label
  58. 3480 try:
  59. -> 3481 slc = self._get_loc_only_exact_matches(label)
  60. 3482 except KeyError as err:
  61. 3483 try:
  62. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in _get_loc_only_exact_matches(self, key)
  63. 3448 get_slice_bound.
  64. 3449 """
  65. -> 3450 return self.get_loc(key)
  66. 3451
  67. 3452 def get_slice_bound(self, label, side, kind):
  68. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
  69. 2442 return self._engine.get_loc(key)
  70. 2443 except KeyError:
  71. -> 2444 return self._engine.get_loc(self._maybe_cast_indexer(key))
  72. 2445
  73. 2446 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
  74. pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5280)()
  75. pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126)()
  76. pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20523)()
  77. pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477)()
  78. KeyError: 'Sp'
  1. # 对college进行排序
  2. In[58]: college = college.sort_index()
  3. In[59]: college = college.head()
  4. Out[59]:

7. 按照字母切片 - 图1

  1. # 再尝试选取字母顺序在‘Sp’和‘Su’之间的学校
  2. In[60]: pd.options.display.max_rows = 6
  3. In[61]: college.loc['Sp':'Su']
  4. Out[61]:

7. 按照字母切片 - 图2

  1. # 可以用is_monotonic_increasing或is_monotonic_decreasing检测字母排序的顺序
  2. In[62]: college = college.sort_index(ascending=False)
  3. college.index.is_monotonic_decreasing
  4. Out[62]: True
  1. # 字母逆序选取
  2. In[63]: college.loc['E':'B']
  3. Out[63]:

7. 按照字母切片 - 图3