I have a pandas series with a 2-level multiindex, and I would like to create entries to a new level-2 index with the same value for all. Let me illustrate:
import pandas as pd
import numpy as np
arrays = [
np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),
np.array(["one", "two", "one", "two", "one", "two", "one", "two"]),
]
s = pd.Series(np.random.randn(8), index=arrays)
print(s)
which yields:
bar one 0.636008
two 0.092757
baz one 0.536576
two -0.135340
foo one 0.095891
two -0.470991
qux one -1.766848
two -1.707228
dtype: float64
How would I now go ahead and create a new entry with the second level index three and set all of them to 0 without iterating through the first-level indeces.
s.loc[(slice(None), 'three')] = 0 was my first try, but did not work.