按可调用性选择

New in version 0.18.1.

.loc, .iloc, and also [] indexing can accept a callable as indexer. The callable must be a function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing.

  1. In [88]: df1 = pd.DataFrame(np.random.randn(6, 4),
  2. ....: index=list('abcdef'),
  3. ....: columns=list('ABCD'))
  4. ....:
  5. In [89]: df1
  6. Out[89]:
  7. A B C D
  8. a -0.023688 2.410179 1.450520 0.206053
  9. b -0.251905 -2.213588 1.063327 1.266143
  10. c 0.299368 -0.863838 0.408204 -1.048089
  11. d -0.025747 -0.988387 0.094055 1.262731
  12. e 1.289997 0.082423 -0.055758 0.536580
  13. f -0.489682 0.369374 -0.034571 -2.484478
  14. In [90]: df1.loc[lambda df: df.A > 0, :]
  15. Out[90]:
  16. A B C D
  17. c 0.299368 -0.863838 0.408204 -1.048089
  18. e 1.289997 0.082423 -0.055758 0.536580
  19. In [91]: df1.loc[:, lambda df: ['A', 'B']]
  20. Out[91]:
  21. A B
  22. a -0.023688 2.410179
  23. b -0.251905 -2.213588
  24. c 0.299368 -0.863838
  25. d -0.025747 -0.988387
  26. e 1.289997 0.082423
  27. f -0.489682 0.369374
  28. In [92]: df1.iloc[:, lambda df: [0, 1]]
  29. Out[92]:
  30. A B
  31. a -0.023688 2.410179
  32. b -0.251905 -2.213588
  33. c 0.299368 -0.863838
  34. d -0.025747 -0.988387
  35. e 1.289997 0.082423
  36. f -0.489682 0.369374
  37. In [93]: df1[lambda df: df.columns[0]]
  38. Out[93]:
  39. a -0.023688
  40. b -0.251905
  41. c 0.299368
  42. d -0.025747
  43. e 1.289997
  44. f -0.489682
  45. Name: A, dtype: float64

You can use callable indexing in Series.

  1. In [94]: df1.A.loc[lambda s: s > 0]
  2. Out[94]:
  3. c 0.299368
  4. e 1.289997
  5. Name: A, dtype: float64

Using these methods / indexers, you can chain data selection operations without using temporary variable.

  1. In [95]: bb = pd.read_csv('data/baseball.csv', index_col='id')
  2. In [96]: (bb.groupby(['year', 'team']).sum()
  3. ....: .loc[lambda df: df.r > 100])
  4. ....:
  5. Out[96]:
  6. stint g ab r h X2b X3b hr rbi sb cs bb so ibb hbp sh sf gidp
  7. year team
  8. 2007 CIN 6 379 745 101 203 35 2 36 125.0 10.0 1.0 105 127.0 14.0 1.0 1.0 15.0 18.0
  9. DET 5 301 1062 162 283 54 4 37 144.0 24.0 7.0 97 176.0 3.0 10.0 4.0 8.0 28.0
  10. HOU 4 311 926 109 218 47 6 14 77.0 10.0 4.0 60 212.0 3.0 9.0 16.0 6.0 17.0
  11. LAN 11 413 1021 153 293 61 3 36 154.0 7.0 5.0 114 141.0 8.0 9.0 3.0 8.0 29.0
  12. NYN 13 622 1854 240 509 101 3 61 243.0 22.0 4.0 174 310.0 24.0 23.0 18.0 15.0 48.0
  13. SFN 5 482 1305 198 337 67 6 40 171.0 26.0 7.0 235 188.0 51.0 8.0 16.0 6.0 41.0
  14. TEX 2 198 729 115 200 40 4 28 115.0 21.0 4.0 73 140.0 4.0 5.0 2.0 8.0 16.0
  15. TOR 4 459 1408 187 378 96 2 58 223.0 4.0 2.0 190 265.0 16.0 12.0 4.0 16.0 38.0