0

I have a data frame like this with multi index:

enter image description here

I have the following multiindex:

MultiIndex([('2019-09', 1617),
            ('2019-09', 1618),
            ('2019-09', 1619),
            ('2019-09', 1646),
            ('2019-09', 1655),
            ('2019-09', 1665),
            ('2019-09', 1670),
            ('2019-09', 1674),
            ('2019-09', 1681),
            ('2019-09', 1692),
            ('2019-09', 1739),
            ('2019-09', 1761),
            ('2019-09', 1768),
            ('2019-09', 1782),
            ('2019-10', 1619),
            ('2019-10', 1646),
            ('2019-10', 1782),
            ('2019-10', 1783),
            ('2019-10', 1790),
            ('2019-10', 1800),
            ('2019-10', 1815),
            ('2019-10', 1819),
            ('2019-10', 1826),
            ('2019-10', 1854),
            ('2019-10', 1867),
            ('2019-10', 1883),
            ('2019-10', 1884),
            ('2019-10', 1910),
            ('2019-10', 1916),
            ('2019-11', 1783),
            ('2019-11', 1910),
            ('2019-11', 1959),
            ('2019-11', 1960),
            ('2019-11', 1966),
            ('2019-11', 1986),
            ('2019-11', 2035),
            ('2019-11', 2071),
            ('2019-12', 1783),
            ('2019-12', 1962),
            ('2019-12', 2112),
            ('2019-12', 2118),
            ('2019-12', 2144),
            ('2019-12', 2162),
            ('2019-12', 2182),
            ('2019-12', 2205),
            ('2020-01', 1783),
            ('2020-01', 2112),
            ('2020-01', 2281),
            ('2020-01', 2287),
            ('2020-01', 2307),
            ('2020-01', 2314),
            ('2020-01', 2331),
            ('2020-01', 2333),
            ('2020-01', 2341),
            ('2020-01', 2342),
            ('2020-01', 2351),
            ('2020-01', 2362),
            ('2020-01', 2364),
            ('2020-01', 2388),
            ('2020-01', 2423),
            ('2020-02', 2304),
            ('2020-02', 2366),
            ('2020-02', 2388),
            ('2020-02', 2438),
            ('2020-02', 2987),
            ('2020-02', 2988),
            ('2020-02', 2991),
            ('2020-02', 3003),
            ('2020-02', 3040),
            ('2020-02', 3055),
            ('2020-02', 3071),
            ('2020-02', 3076),
            ('2020-02', 3086),
            ('2020-02', 3115),
            ('2020-03', 3126),
            ('2020-03', 3739),
            ('2020-03', 3748),
            ('2020-03', 3775)],
           names=['createdAt_ym', 'eventId'])

I am trying to get all entries with '2020-02':

df.loc[('2020-02', 'createdAt_ym')]

KeyError: 'createdAt_ym'

I see that the names of the multiindex appear correctly, so I don't understand why I cannot index using

'createdAt_ym'
halo09876
  • 2,725
  • 12
  • 51
  • 71
  • Does this answer your question? [Select rows in pandas MultiIndex DataFrame](https://stackoverflow.com/questions/53927460/select-rows-in-pandas-multiindex-dataframe) – Mayank Porwal Dec 22 '20 at 05:44
  • Check the first answer in the above linked question. That is exactly what you want. – Mayank Porwal Dec 22 '20 at 05:44

1 Answers1

0

If you want to use partial indexing to get all elements with 2020-02 in the first createdAt_ym level, you should be using this syntax:

df.loc['2020-02']
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360