I have a Pandas dataframe df sample with index multi_level:
>>> df STK_Name ROIC mg_r STK_ID RPT_Date 002410 20111231 ??? 0.401 0.956 300204 20111231 ??? 0.375 0.881 300295 20111231 ???? 2.370 0.867 300288 20111231 ???? 1.195 0.861 600106 20111231 ???? 1.214 0.857 300113 20111231 ???? 0.837 0.852
and stk_list defined as stk_list = ['600106','300204','300113']
I want to get df strings, sub_level STK_ID index STK_ID is within stk_list . The output is as follows:
STK_Name ROIC mg_r STK_ID RPT_Date 300204 20111231 ??? 0.375 0.881 600106 20111231 ???? 1.214 0.857 300113 20111231 ???? 0.837 0.852
Basically, I can achieve the goal for this sample data:
df = df.reset_index() ; df[df.STK_ID.isin(stk_list)]
But I already have the "STK_ID" and "RPT_Date" columns in my application data frame, so reset_index () will result in an error. Anyway, I want to filter the index directly instead of columns.
Learn from this: How to filter by sub-level index in Pandas
I try df[df.index.map(lambda x: x[0].isin(stk_list))] , and Pandas 0.8.1 gives AttributeError: 'unicode' object has no attribute 'isin' ,
My question is: how do I filter the Pandas dataframe rows by checking the sublayer index value in the list without using the reset_index() and set_index() methods?
bigbug
source share