Take的方法

Similar to NumPy ndarrays, pandas Index, Series, and DataFrame also provides the take method that retrieves elements along a given axis at the given indices. The given indices must be either a list or an ndarray of integer index positions. take will also accept negative integers as relative positions to the end of the object.

  1. In [108]: index = pd.Index(np.random.randint(0, 1000, 10))
  2. In [109]: index
  3. Out[109]: Int64Index([214, 502, 712, 567, 786, 175, 993, 133, 758, 329], dtype='int64')
  4. In [110]: positions = [0, 9, 3]
  5. In [111]: index[positions]
  6. Out[111]: Int64Index([214, 329, 567], dtype='int64')
  7. In [112]: index.take(positions)
  8. Out[112]: Int64Index([214, 329, 567], dtype='int64')
  9. In [113]: ser = pd.Series(np.random.randn(10))
  10. In [114]: ser.iloc[positions]
  11. Out[114]:
  12. 0 -0.179666
  13. 9 1.824375
  14. 3 0.392149
  15. dtype: float64
  16. In [115]: ser.take(positions)
  17. Out[115]:
  18. 0 -0.179666
  19. 9 1.824375
  20. 3 0.392149
  21. dtype: float64

For DataFrames, the given indices should be a 1d list or ndarray that specifies row or column positions.

  1. In [116]: frm = pd.DataFrame(np.random.randn(5, 3))
  2. In [117]: frm.take([1, 4, 3])
  3. Out[117]:
  4. 0 1 2
  5. 1 -1.237881 0.106854 -1.276829
  6. 4 0.629675 -1.425966 1.857704
  7. 3 0.979542 -1.633678 0.615855
  8. In [118]: frm.take([0, 2], axis=1)
  9. Out[118]:
  10. 0 2
  11. 0 0.595974 0.601544
  12. 1 -1.237881 -1.276829
  13. 2 -0.767101 1.499591
  14. 3 0.979542 0.615855
  15. 4 0.629675 1.857704

It is important to note that the take method on pandas objects are not intended to work on boolean indices and may return unexpected results.

  1. In [119]: arr = np.random.randn(10)
  2. In [120]: arr.take([False, False, True, True])
  3. Out[120]: array([-1.1935, -1.1935, 0.6775, 0.6775])
  4. In [121]: arr[[0, 1]]
  5. Out[121]: array([-1.1935, 0.6775])
  6. In [122]: ser = pd.Series(np.random.randn(10))
  7. In [123]: ser.take([False, False, True, True])
  8. Out[123]:
  9. 0 0.233141
  10. 0 0.233141
  11. 1 -0.223540
  12. 1 -0.223540
  13. dtype: float64
  14. In [124]: ser.iloc[[0, 1]]
  15. Out[124]:
  16. 0 0.233141
  17. 1 -0.223540
  18. dtype: float64

Finally, as a small note on performance, because the take method handles a narrower range of inputs, it can offer performance that is a good deal faster than fancy indexing.