“Too many indexers” with DataFrame.loc

后端 未结 2 1702
栀梦
栀梦 2020-12-02 20:44

I\'ve read the docs about slicers a million times, but have never got my head round it, so I\'m still trying to figure out how to use loc to slice a DataF

相关标签:
2条回答
  • 2020-12-02 21:09

    The reason this doesn't work is tied to the need to specify the axis of indexing (mentioned in http://pandas.pydata.org/pandas-docs/stable/advanced.html). An alternative solution to your problem is to simply do this:

    df.loc(axis=0)[:, :, 'C1', :]
    

    Pandas gets confused sometimes when indexes are similar or contain similar values. If you were to have a column named 'C1' or something you would also need to do this under this style of slicing/selecting.

    0 讨论(0)
  • 2020-12-02 21:21

    To be safe (in the sense: this will work in all cases), you need to index both row index and columns, for which you can use pd.IndexSlice to do this easily:

    In [26]: idx = pd.IndexSlice
    
    In [27]: df.loc[idx[:, :, 'C1', :],:]
    Out[27]:
                               value
    first second third fourth
    A0    B0     C1    D0          2
                       D1          3
          B1     C1    D0         10
                       D1         11
    A1    B0     C1    D0         18
                       D1         19
          B1     C1    D0         26
                       D1         27
    A2    B0     C1    D0         34
                       D1         35
          B1     C1    D0         42
                       D1         43
    A3    B0     C1    D0         50
                       D1         51
          B1     C1    D0         58
                       D1         59
    

    Here idx[:, :, 'C1', :] is an easier way to write [slice(None), slice(None),'C1', slice(None)]. Instead of pd.IndexSlice, you can also use np.s_ which is a bit shorter.

    The reason that the other ones work, I am not fully sure of. But see the note in the documentation here: http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers (the first red warning box) where it is stated that:

    You should specify all axes in the .loc specifier, meaning the indexer for the index and for the columns. Their are some ambiguous cases where the passed indexer could be mis-interpreted as indexing both axes, rather than into say the MuliIndex for the rows.

    0 讨论(0)
提交回复
热议问题