Return Pandas multiindex as list of tuples?

Either df.index.tolist() or df.index.values will do.


From pandas >= 0.24, you can also use df.index.to_numpy().


In pandas 0.24+ is possible use MultiIndex.to_flat_index:

print (df.index.to_flat_index())

Sample:

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)

print (s.index.to_flat_index())
Index([('bar', 'one'), ('bar', 'two'), ('baz', 'one'), ('baz', 'two'),
       ('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')],
      dtype='object')

Tags:

Python

Pandas