Following Problem I got a DataFrame like so:
df = pd.DataFrame(columns=pd.MultiIndex(levels=[["foo","baa"],["x","y","z"]], labels=[[0,0,0,1,1,1],[0,1,2,0,1,2]]), index=[0,1,2,3], data=np.random.rand(4,6))
foo baa
x y z x y z
0 0.263780 0.942880 0.176744 0.914854 0.920735 0.573692
1 0.601317 0.584691 0.288536 0.832064 0.095142 0.186045
2 0.807323 0.075620 0.399703 0.936894 0.168441 0.468984
3 0.871293 0.324817 0.395784 0.133541 0.365586 0.615219
And I want to get say foo out of it into a new DataFrame completly empty constructed like this:
r_df = pd.DataFrame(index=df.index)
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]
it should look like this:
foo
x y z
0 0.263780 0.942880 0.176744
1 0.601317 0.584691 0.288536
2 0.807323 0.075620 0.399703
3 0.871293 0.324817 0.395784
How I tryed to do it was like this:
for label in df.columns.levels[0]:
data = df[label]
for row in data.index:
r_df.loc[row,(label,"x")] = data["x"]
r_df.loc[row,(label,"y")] = data["y"]
r_df.loc[row,(label,"z")] = data["z"]
I thought this could work because multi index can be referenced by tubles but it didnt.
r_df = df.xs('foo', axis=1, level=0, drop_level=False)if you do want to keep it as a multiindex.