I have a Multi-Index data frame and I want to add a column in level 1 and have it grouped in the appropriate level 0 column. When I assign the new column, it appends it to the end of the df.
In [28]: df
Out[28]:
first qux bar foo
second one two one two one two
A -0.563477 -0.032948 -0.131031 1.110537 -0.541374 0.760088
B -1.767642 -1.305016 -0.786291 -0.396981 1.983372 -0.106018
C -0.471136 0.616730 0.019877 0.910230 0.352304 -0.361370
In [29]: df['qux','three'] = [1,2,3]
In [30]: df
Out[30]:
first qux bar foo qux
second one two one two one two three
A -0.563477 -0.032948 -0.131031 1.110537 -0.541374 0.760088 1
B -1.767642 -1.305016 -0.786291 -0.396981 1.983372 -0.106018 2
C -0.471136 0.616730 0.019877 0.910230 0.352304 -0.361370 3
What I WANT it to look like is
first qux bar foo
second one two three one two one two
A -0.563477 -0.032948 1 -0.131031 1.110537 -0.541374 0.760088
B -1.767642 -1.305016 2 -0.786291 -0.396981 1.983372 -0.106018
C -0.471136 0.616730 3 0.019877 0.910230 0.352304 -0.361370
I tried df.sort_index(axis=1,level=0), which at least grouped the qux's together, but it alphabetized my level 0 headings. How can I get it to group the common column names without alphabetizing them?