Skip to content Skip to sidebar Skip to footer

Pandas/Python Multiply Columns By Row

Apologies if this is a simple question. I have two dataframes each with the same columns. I need to multiply each row in the second dataframe by the only row in the first. Eventu

Solution 1:

You can use mul by first row of df1 selected by iloc:

print (df2.mul(df1.iloc[0]))

Sample:

print (df1)
      51200000.0  70000000.0
age                        
0          0.75        0.25

print (df2)
      51200000.0  70000000.0
age                         
91.0         1.0         2.0
94.0         5.0        10.0
96.0         0.0         0.0

print (df2.mul(df1.iloc[0]))
      51200000.0  70000000.0
age                         
91.0        0.75         0.5
94.0        3.75         2.5
96.0        0.00         0.0

Post a Comment for "Pandas/Python Multiply Columns By Row"