0

I have a dataframe df as:

node    date_   A1                 A2            
bkt             B1         B2      B1         B2   
0   1/1/2015    0.9        1       2          1
1   1/2/2015    0.7        0.6     5          6
2   1/3/2015    0.9        1       9          23

df.columns
MultiIndex([( 'date_', ''),
            ( 'A1', 'B1'),
            ( 'A1', 'B2'),
            ( 'A2', 'B1'),
            ( 'A2',  'B2'),
            ( 'Month', '')],
           names=['node', 'bkt'])

I have another dataframe df2 as:

bkt             B1       B2  
0   1/1/2015    2        1   
1   1/2/2015    2        2
2   1/3/2015    3        1   

Is it possible to multiply df with df1 so that corresponding columns B1 and B2 get multiplied to give the following results:

node    date_   A1                 A2            
bkt             B1         B2      B1         B2   
0   1/1/2015    1.8        1       4          1
1   1/2/2015    1.4        1.2     10         12
2   1/3/2015    2.7        1       27         23
Zanam
  • 3,540
  • 9
  • 42
  • 91

1 Answers1

3

Yes, you can:

df_out = df.copy()
df_out.loc[:,['A1','A2']] = df.mul(df2, level=1, axis='columns')

Output:

node     date_   A1       A2    
bkt              B1   B2  B1  B2
0                               
0     1/1/2015  1.8  1.0   4   1
1     1/2/2015  1.4  1.2  10  12
2     1/3/2015  2.7  1.0  27  23
Quang Hoang
  • 117,517
  • 10
  • 34
  • 52