I have the following table:
JavaScript
x
30
30
1
NSRCODE PBL_AWI Area
2
CM BONS 44705.492941
3
BTNN 253854.591990
4
FONG 41625.590370
5
FONS 16814.159680
6
Lake 57124.819333
7
River 1603.906642
8
SONS 583958.444751
9
STNN 45603.837177
10
clearcut 106139.013930
11
disturbed 127719.865675
12
lowland 118795.578059
13
upland 2701289.270193
14
LBH BFNN 289207.169650
15
BONS 9140084.716743
16
BTNI 33713.160390
17
BTNN 19748004.789040
18
FONG 1687122.469691
19
FONS 5169959.591270
20
FTNI 317251.976160
21
FTNN 6536472.869395
22
Lake 258046.508310
23
River 44262.807900
24
SONS 4379097.677405
25
burn regen 744773.210860
26
clearcut 54066.756790
27
disturbed 597561.471686
28
lowland 12591619.141842
29
upland 23843453.638117
30
Note: Both NSRCODE
and PBL_AWI
are indices.
How do I search for values in column PBL_AWI
? For example I want to keep the values ['Lake', 'River', 'Upland']
.
Advertisement
Answer
You can get_level_values
in conjunction with Boolean slicing.
JavaScript
1
10
10
1
In [50]:
2
3
print df[np.in1d(df.index.get_level_values(1), ['Lake', 'River', 'Upland'])]
4
Area
5
NSRCODE PBL_AWI
6
CM Lake 57124.819333
7
River 1603.906642
8
LBH Lake 258046.508310
9
River 44262.807900
10
The same idea can be expressed in many different ways, such as df[df.index.get_level_values('PBL_AWI').isin(['Lake', 'River', 'Upland'])]
Note that you have 'upland'
in your data instead of 'Upland'