How to get away with a multidimensional index in pandas

前端 未结 2 1518
暖寄归人
暖寄归人 2021-01-02 12:33

In Pandas, what is a good way to select sets of arbitrary rows in a multiindex?

df = pd.DataFrame(columns=[\'A\', \'B\', \'C\'])
df[\'A\'] = [\'a\', \'a\', \         


        
2条回答
  •  粉色の甜心
    2021-01-02 13:38

    You indeed cannot index with a DataFrame directly, but if you convert it to an Index object, it does the correct thing (a row in the DataFrame will be regarded as one multi-index entry):

    In [43]: pd.Index(the_indices_we_want)
    Out[43]: Index([(u'a', 1), (u'b', 4)], dtype='object')
    
    In [44]: df.ix[pd.Index(the_indices_we_want)]
    Out[44]:
         C
    A B
    a 1  1
    b 4  4
    
    In [45]: df.ix[[tuple(x) for x in the_indices_we_want.values]]
    Out[45]:
         C
    A B
    a 1  1
    b 4  4
    

    This is a somewhat cleaner. And with some quick tests it seems to be a bit faster (but not much, only 2 times)

提交回复
热议问题