Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering multiple items in a multi-index Python Panda dataframe

I have the following table:

Note: Both NSRCODE and PBL_AWI are index's

Note: the % Of area column would be filled out just have not done so yet.

NSRCODE  PBL_AWI          Area           % Of Area CM       BONS             44705.492941          BTNN            253854.591990          FONG             41625.590370          FONS             16814.159680          Lake             57124.819333          River             1603.906642          SONS            583958.444751          STNN             45603.837177          clearcut        106139.013930          disturbed       127719.865675          lowland         118795.578059          upland         2701289.270193 LBH      BFNN            289207.169650          BONS           9140084.716743          BTNI             33713.160390          BTNN          19748004.789040          FONG           1687122.469691          FONS           5169959.591270          FTNI            317251.976160          FTNN           6536472.869395          Lake            258046.508310          River            44262.807900          SONS           4379097.677405          burn regen      744773.210860          clearcut         54066.756790          disturbed       597561.471686          lowland       12591619.141842          upland        23843453.638117 

How do I filter out item in the "PBL_AWI" index? For example I want to keep ['Lake', 'River', 'Upland']

like image 728
Tristan Forward Avatar asked Aug 10 '14 00:08

Tristan Forward


People also ask

How do you filter a Dataframe in multiple conditions?

Using Loc to Filter With Multiple Conditions The loc function in pandas can be used to access groups of rows or columns by label. Add each condition you want to be included in the filtered result and concatenate them with the & operator. You'll see our code sample will return a pd. dataframe of our filtered rows.

How do you filter a Dataframe in Python based on multiple column values?

To filter pandas DataFrame by multiple columns. When we filter a DataFrame by one column, we simply compare that column values against a specific condition but when it comes to filtering of DataFrame by multiple columns, we need to use the AND (&&) Operator to match multiple columns with multiple conditions.


1 Answers

You can get_level_values in conjunction with Boolean slicing.

In [50]:  print df[np.in1d(df.index.get_level_values(1), ['Lake', 'River', 'Upland'])]                           Area NSRCODE PBL_AWI                CM      Lake      57124.819333         River      1603.906642 LBH     Lake     258046.508310         River     44262.807900 

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'

like image 91
CT Zhu Avatar answered Sep 28 '22 02:09

CT Zhu