I have a multiindex df s:
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'])
pd.MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],
labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],
names=['first', 'second'])
s = pd.Series(np.random.randn(8), index=index)
s
I want to add a new index column "zero" with x,y,z to s by matching index columns "first" and "second". In other words, I want to repeat s three times, but with this additional index column with x,y,z. I tried the reindex (see below), but why it gives me all NaNs?
mux=pd.MultiIndex.from_product([["x","y","z"],
s.index.get_level_values(0),
s.index.get_level_values(1)],
names=["zero","first", "second"])
t=s.reindex(mux)
t
I also tried specifying matching levels to be "first" and "second", but it looks like level only takes one integer?