-2

The following example from the docs works as expected:

s = pd.Series([1, 2, 3, 4, 5, 6],index=pd.MultiIndex.from_product([["A", "B"], ["c", "d", "e"]]))

s['A']

c    1
d    2
e    3

However, for this example, from my data, such indexing raises an error:

df = pd.DataFrame({'client_id': {('foo', '2018-01-29'): '1',
  ('bar', '2018-01-29'): '1',
  ('baz', '2018-01-29'): '1',
  ('alice', '2018-01-29'): '1',
  ('bob', '2018-01-29'): '1'}})

df['alice']

KeyError: 'alice'

What am I doing wrong?

cs95
  • 379,657
  • 97
  • 704
  • 746
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75

1 Answers1

1

Just use loc:

df.loc['alice']

           client_id
2018-01-29         1

It is not clear to pandas whether "alice" is a column or not with df. With the series, it is clear a call to __getitem__ is accessing the index.


Other alternatives (as per How do I slice or filter MultiIndex DataFrame levels?):

df.loc(axis=0)['alice']

           client_id
2018-01-29         1

df.xs('alice')

           client_id
2018-01-29         1

df.query('ilevel_0 == "alice"')

                 client_id
alice 2018-01-29         1
cs95
  • 379,657
  • 97
  • 704
  • 746