Pandas slicing along multiindex and separate indices

后端 未结 2 983
别跟我提以往
别跟我提以往 2021-02-08 20:11

I\'ve started using Pandas for some large Datasets and mostly it works really well. There are some questions I have regarding the indices though

  1. I have a MultiI

相关标签:
2条回答
  • 2021-02-08 20:44

    Edit: This answer for pandas versions lower than 0.10.0 only:

    Okay @hayden had the right idea to start with:

    An index has the method get_level_values() which returns, however, an array (in pandas versions < 0.10.0). The isin() method doesn't exist for arrays but this works:

    from pandas import lib
    lib.ismember(df.index.get_level_values('a'), set([5, 7, 10, 13])
    

    That only answers question 1 - but I'll give an update if I crack 2, 3 (half done with @hayden's help)

    0 讨论(0)
  • 2021-02-08 20:54

    For the first part, you can use boolean indexing using get_level_values:

    df[df.index.get_level_values('a').isin([5, 7, 10, 13])]
    

    For the second two, you can inspect the MultiIndex object by calling:

    df.index
    

    (and this can be inspected/sliced.)

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