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
labelsshould becodes), the representation of the MultiIndex does not show the levels/codes:Output:
If you want the levels and codes, use: