Pandas: Elementwise multiplication of two dataframes Pandas: Elementwise multiplication of two dataframes python python

Pandas: Elementwise multiplication of two dataframes


In [161]: pd.DataFrame(df.values*df2.values, columns=df.columns, index=df.index)Out[161]:    col1  col2  col31    10   200  30002    10   200  30003    10   200  30004    10   200  30005    10   200  3000


A simpler way to do this is just to multiply the dataframe whose colnames you want to keep with the values (i.e. numpy array) of the other, like so:

In [63]: df * df2.valuesOut[63]:    col1  col2  col31    10   200  30002    10   200  30003    10   200  30004    10   200  30005    10   200  3000

This way you do not have to write all that new dataframe boilerplate.


To utilize Pandas broadcasting properties, you can use multiply.

df.multiply(df3['col1'], axis=0)