I am learning about pandas MultiIndex()
for the first time. Here I have two lists that I want to combine using the MultiIndex()
function. the outcome I am expecting should have levels
and labels
but this is not the case here.
here is my code:
import numpy as np
import pandas as pd
inside = ['a', 'a', 'a', 'b', 'b', 'b']
outside = [1,2,3,1,2,3]
zipped = list(zip(inside, outside))
hier_zipped = pd.MultiIndex.from_tuples(zipped)
hier_zipped
the expected outcome:
MultiIndex(levels = [['a','b'],[1,2,3]],
labels = [[0,0,0,1,1,1],[0,1,2,0,1,2]]
)
the actual outcome:
MultiIndex([('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('b', 3)],
)
Your two codes are giving equal objects (Just note that labels
should be codes
), the representation of the MultiIndex does not show the levels/codes:
pd.MultiIndex(levels=[['a','b'],[1,2,3]], codes=[[0,0,0,1,1,1],[0,1,2,0,1,2]])
Output:
MultiIndex([('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('b', 3)],
)
If you want the levels and codes, use:
hier_zipped.levels
# FrozenList([['a', 'b'], [1, 2, 3]]),
hier_zipped.codes
# FrozenList([[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]])