“Too many indexers” with DataFrame.loc

后端 未结 2 1777
栀梦
栀梦 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.

提交回复
热议问题