简体   繁体   中英

Add multiple columns at once to multiindex Pandas dataframe

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.

  • Multi-column names are passed as Tuples , like df[('Foo', 'End')] .
import pamadas as pd

# test data
col = pd.MultiIndex.from_arrays([['Foo', 'Bar'], ['Begin', 'Begin']], names=['First', 'Second'])
df = pd.DataFrame([[5, 1], [4, 4], [6, 6]], columns=col)
new = pd.DataFrame({'Foo': [1, 5, 7], 'Bar': [2, 4, 7]})

# write new columns
df[('Foo', 'End')] = new['Foo']
df[('Bar', 'End')] = new['Bar']

# display(df)
First    Foo   Bar Foo Bar
Second Begin Begin End End
0          5     1   1   2
1          4     4   5   4
2          6     6   7   7

For many columns

  • col , column name in new , must correspond to the top level column name in df .
for col in new.columns:
    df[(col, 'new col name')] = new[col]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM