I have a dataframe multiindex pandas dataframe df
First Foo Bar
Second Begin Begin
1 5 1
2 4 4
3 6 6
And I want to add two columns of the same name
First Foo Bar
Second Begin End Begin End
1 5 1 1 2
2 4 5 4 4
3 6 7 6 7
From this source (new
):
First Foo Bar
1 1 2
2 5 4
3 7 7
I tried things like df[:] = new[:]
but this returned only NaN
An alternative would be to use something like a for-loop but that's not the Pandas approach. Searching the web did not give me any insights as to solving this problem.
How can I add new columns with the same name and shape to every first level of a multiindex Pandas dataframe?
Edit:
This approach df[('Foo', 'End')] = new['Foo'] df[('Bar', 'End')] = new['Bar']
is not an option because in my actual problem there is not two columns to be added, but hundreds of columns.