Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Elementwise multiplication of two dataframes

Tags:

I know how to do element by element multiplication between two Pandas dataframes. However, things get more complicated when the dimensions of the two dataframes are not compatible. For instance below df * df2 is straightforward, but df * df3 is a problem:

df = pd.DataFrame({'col1' : [1.0] * 5,                     'col2' : [2.0] * 5,                     'col3' : [3.0] * 5 }, index = range(1,6),) df2 = pd.DataFrame({'col1' : [10.0] * 5,                      'col2' : [100.0] * 5,                      'col3' : [1000.0] * 5 }, index = range(1,6),) df3 = pd.DataFrame({'col1' : [0.1] * 5}, index = range(1,6),)  df.mul(df2, 1) # element by element multiplication no problems  df.mul(df3, 1) # df(row*col) is not equal to df3(row*col)    col1  col2  col3 1   0.1   NaN   NaN 2   0.1   NaN   NaN 3   0.1   NaN   NaN 4   0.1   NaN   NaN 5   0.1   NaN   NaN 

In the above situation, how can I multiply every column of df with df3.col1?

My attempt: I tried to replicate df3.col1 len(df.columns.values) times to get a dataframe that is of the same dimension as df:

df3 = pd.DataFrame([df3.col1 for n in range(len(df.columns.values)) ]) df3         1    2    3    4    5 col1  0.1  0.1  0.1  0.1  0.1 col1  0.1  0.1  0.1  0.1  0.1 col1  0.1  0.1  0.1  0.1  0.1 

But this creates a dataframe of dimensions 3 * 5, whereas I am after 5*3. I know I can take the transpose with df3.T() to get what I need but I think this is not that the fastest way.

like image 285
Zhubarb Avatar asked Jan 09 '14 14:01

Zhubarb


People also ask

How do pandas multiply values?

multiply() function perform the multiplication of series and other, element-wise. The operation is equivalent to series * other , but with support to substitute a fill_value for missing data in one of the inputs.

How do you multiply two Series in pandas?

Multiplying of two pandas. Series objects can be done through applying the multiplication operator “*” as well. Through mul() method, handling None values in the data is possible by replacing them with a default value using the parameter fill_value.

Does pandas do matrix multiplication?

The dot() method of pandas DataFrame class does a matrix multiplication between a DataFrame and another DataFrame, a pandas Series or a Python sequence and returns the resultant matrix.

What does .values in pandas do?

The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.


1 Answers

In [161]: pd.DataFrame(df.values*df2.values, columns=df.columns, index=df.index) Out[161]:     col1  col2  col3 1    10   200  3000 2    10   200  3000 3    10   200  3000 4    10   200  3000 5    10   200  3000 
like image 128
unutbu Avatar answered Oct 04 '22 03:10

unutbu