2.17 The select()
Method
Another way to extract slices from an object is with the select
method of
Series, DataFrame, and Panel. This method should be used only when there is no
more direct way. select
takes a function which operates on labels along
axis
and returns a boolean. For instance:
In [1]: df
Out[1]:
A B C D
2000-01-01 -0.5146 -0.4496 1.7346 0.6434
2000-01-02 0.0261 0.0804 -0.7974 -0.6281
2000-01-03 -0.3462 0.9681 0.7056 -2.1567
2000-01-04 0.9506 0.5382 -0.4508 0.5508
2000-01-05 -0.7596 1.2238 -0.1598 -1.4558
2000-01-06 1.2085 -1.7215 -0.0059 0.8703
2000-01-07 -1.2655 -1.4048 -0.2995 0.9627
2000-01-08 0.1223 1.4023 -0.4961 -0.6335
In [2]: df.select(lambda x: x == 'A', axis=1)
Out[2]:
A
2000-01-01 -0.5146
2000-01-02 0.0261
2000-01-03 -0.3462
2000-01-04 0.9506
2000-01-05 -0.7596
2000-01-06 1.2085
2000-01-07 -1.2655
2000-01-08 0.1223