title: 初识Pandas系列四:数据访问
hidden: true
hide: true
categories:


Pandas Index索引的用途可以总结为:

  • 更方便的数据查询;
  • 使用index可以获得性能提升;
  • 自动的数据对齐功能;
  • 更多更强大的数据结构支持;

Pandas支持3种类型的多轴索引,即.loc.iloc,和[].ix,在0.20.0版本之后,ix被弃用,可按照需求采用.loc.iloc索引数据)。

.loc

主要是基于标签的,但也可以与布尔数组一起使用。.loc()具有多种访问方式,如 :

  • 单个标签,例如5'a'(注意,整数是有效的标签,但它们是指标签而不是位置)。
  • 列表或标签数组。['a', 'b', 'c']
  • 带标签的切片对象'a':'f'(切片时,也包括起始边界)
  • 布尔数组
  • 一个callable带有一个参数的函数(调用Series或DataFrame)并返回有效的索引输出(以上的其中一种)。

.iloc

主要是基于整数位置的,但也可以使用布尔值阵列。具有多种访问方式,如 :

  • 一个整数,例如5
  • 整数列表或数组。[4, 3, 0]
  • 带有整数的切片对象1:7
  • 布尔数组。
  • 一个callable带有一个参数的函数(调用Series或DataFrame)并返回有效的索引输出(以上的其中一种)。

at

iat

isin

notin

where

  1. s = pd.Series(data=np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
  2. print(s)
  3. """
  4. a 0.036334
  5. b -1.705168
  6. c -1.846465
  7. d 1.350649
  8. e 0.055091
  9. dtype: float64
  10. """
  11. print(s[s < 0])
  12. """
  13. b -1.705168
  14. c -1.846465
  15. dtype: float64
  16. """
  17. print(s.where(s < 0))
  18. """
  19. a NaN
  20. b -1.705168
  21. c -1.846465
  22. d NaN
  23. e NaN
  24. dtype: float64
  25. """
  26. df = pd.DataFrame(np.random.randn(5, 4), columns=['A', 'B', 'C', 'D'])
  27. print(df)
  28. """
  29. A B C D
  30. 0 -0.080725 0.389280 0.859920 3.031320
  31. 1 -0.652758 0.461458 -0.680639 2.072086
  32. 2 -0.647795 0.978124 0.144277 -0.638548
  33. 3 -1.593368 -0.040756 -0.488344 0.079780
  34. 4 -2.915217 -0.212025 -1.311406 1.200810
  35. """
  36. print(df[df < 0])
  37. """
  38. A B C D
  39. 0 -0.080725 NaN NaN NaN
  40. 1 -0.652758 NaN -0.680639 NaN
  41. 2 -0.647795 NaN NaN -0.638548
  42. 3 -1.593368 -0.040756 -0.488344 NaN
  43. 4 -2.915217 -0.212025 -1.311406 NaN
  44. """
  45. print(df.where(df < 0))
  46. """
  47. A B C D
  48. 0 -0.080725 NaN NaN NaN
  49. 1 -0.652758 NaN -0.680639 NaN
  50. 2 -0.647795 NaN NaN -0.638548
  51. 3 -1.593368 -0.040756 -0.488344 NaN
  52. 4 -2.915217 -0.212025 -1.311406 NaN
  53. """

query

DataFrame提供query方法允许使用表达式进行数据选取。

  1. df = pd.DataFrame(np.random.rand(5, 3), columns=['A', 'B', 'C'])
  2. print(df)
  3. """
  4. A B C
  5. 0 0.262805 0.960896 0.276461
  6. 1 0.088881 0.169215 0.465360
  7. 2 0.678148 0.492864 0.137100
  8. 3 0.494619 0.765754 0.261780
  9. 4 0.675247 0.752663 0.709871
  10. """
  11. print(df[(df.A < df.B) & (df.B < df.C)])
  12. """
  13. A B C
  14. 1 0.088881 0.169215 0.46536
  15. """
  16. print(df[(df.A < df.B) & (df.B < df.C)] == df.query('(A<B<C)'))
  17. # df[(df.A < df.B) & (df.B < df.C)]等同于 df.query('(A<B) & (B<C)')
  18. """
  19. A B C
  20. 1 True True True
  21. """

如果没有名称的列,则执行相同的操作但返回索引名称a

  1. df = pd.DataFrame(np.random.rand(5, 3), columns=['A', 'B', 'C'])
  2. df.index.name = 'a'
  3. print(df)
  4. """
  5. A B C
  6. a
  7. 0 0.516509 0.529194 0.965933
  8. 1 0.922649 0.382211 0.383656
  9. 2 0.710264 0.601778 0.386659
  10. 3 0.199166 0.616787 0.817979
  11. 4 0.404193 0.077953 0.319721
  12. """
  13. print(df.query('( a < B < C )'))
  14. """
  15. A B C
  16. a
  17. 0 0.516509 0.529194 0.965933
  18. """

如果您不希望或不能命名索引,则可以使用 ‘index’:

  1. df = pd.DataFrame(np.random.rand(5, 3), columns=['A', 'B', 'C'])
  2. print(df)
  3. """
  4. A B C
  5. 0 0.231783 0.752778 0.763465
  6. 1 0.313005 0.410736 0.928091
  7. 2 0.607272 0.311927 0.989749
  8. 3 0.104900 0.026600 0.575775
  9. 4 0.523358 0.132647 0.750618
  10. """
  11. print(df.query('(index < B < C)'))
  12. """
  13. A B C
  14. 0 0.231783 0.752778 0.763465
  15. """

如果索引的名称与列名称重叠,则列名称优先;也可以使用特殊标识符 ‘index’

  1. df = pd.DataFrame(np.random.rand(5, 3), columns=['A', 'B', 'C'])
  2. df.index.name = 'A'
  3. print(df)
  4. """
  5. A B C
  6. A
  7. 0 0.202598 0.782984 0.913703
  8. 1 0.749658 0.946730 0.032033
  9. 2 0.240811 0.688982 0.235367
  10. 3 0.041943 0.799996 0.404671
  11. 4 0.471568 0.145648 0.379349
  12. """
  13. print(df.query('A > 0.5'))
  14. """
  15. A B C
  16. A
  17. 1 0.749658 0.94673 0.032033
  18. """
  19. print(df.query('index > 0.5'))
  20. """
  21. A B C
  22. A
  23. 1 0.749658 0.946730 0.032033
  24. 2 0.240811 0.688982 0.235367
  25. 3 0.041943 0.799996 0.404671
  26. 4 0.471568 0.145648 0.379349
  27. """

get

Series或DataFrame都提供一个get,可以返回默认值的方法。

  1. s = pd.Series(data=np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
  2. print(s)
  3. """
  4. a -0.078789
  5. b -0.987079
  6. c 0.347506
  7. d 1.299694
  8. e -1.383272
  9. dtype: float64
  10. """
  11. print(s.get('a')) #-0.07878905486063005,可以设置default=-1
  12. df = pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]), orient='index', columns=['a', 'b', 'c'])
  13. print(df)
  14. """
  15. a b c
  16. A 1 2 3
  17. B 4 5 6
  18. """
  19. print(df.get('a'))
  20. """
  21. A 1
  22. B 4
  23. Name: a, dtype: int64
  24. """

lookup

高级的索引方式,仅适用于DataFrame。给定的行和列标签数组长度是相等的,返回对应于每个(行,列)一对的值的数组。

  1. df = pd.DataFrame(np.random.rand(20, 4), columns=['A', 'B', 'C', 'D'])
  2. print(df)
  3. """
  4. A B C D
  5. 0 0.140680 0.090249 0.856025 0.372685
  6. 1 0.003483 0.212248 0.473694 0.448281
  7. 2 0.275576 0.070727 0.024614 0.649417
  8. 3 0.541142 0.683731 0.452914 0.634844
  9. 4 0.092144 0.531721 0.992580 0.148160
  10. 5 0.435356 0.359170 0.757804 0.872095
  11. 6 0.841636 0.103978 0.781045 0.414116
  12. 7 0.290041 0.817660 0.440186 0.990049
  13. 8 0.533673 0.134341 0.685136 0.790448
  14. 9 0.356519 0.660389 0.498869 0.853177
  15. 10 0.433663 0.894910 0.801654 0.146775
  16. 11 0.591647 0.782684 0.718427 0.646008
  17. 12 0.729917 0.175254 0.493607 0.715460
  18. 13 0.442146 0.721788 0.758316 0.425173
  19. 14 0.304630 0.530807 0.643836 0.071652
  20. 15 0.822386 0.946926 0.151597 0.444338
  21. 16 0.425350 0.726438 0.100328 0.757651
  22. 17 0.066900 0.981867 0.933085 0.487493
  23. 18 0.371977 0.646238 0.793118 0.531412
  24. 19 0.603142 0.479281 0.134921 0.202139
  25. """
  26. print(df.lookup(list(range(0, 10, 2)), ['B', 'C', 'A', 'B', 'D']))
  27. #行的范围为0~10(不包含10),2为间隔,提取的行为0,2,4,6,8,对应的列数组为BCABD,所要提取的数据索引为B0,C2,A4,B6,D8
  28. """
  29. [0.09024937 0.02461377 0.0921435 0.10397808 0.79044818]
  30. """