Search code examples
pythonpandasdataframematrix-multiplication

Vectorized dot product in pandas?


I have two dataframes, df1 is indexed by date and contains some numeric values val1, val2 for products/entries A,B,...:

Date       entry  val1  val2
2017-04-12    A     1     10
2017-04-12    B     2     10
2017-04-12    C     3     10
2017-04-13    A     1     20
2017-04-13    B     2     20
2017-04-13    D     3     20

df2 has coefficients for each value for each date:

     2017-04-12  2017-04-13
val1     4           6
val2     5           7

Is there a nice vectorized way of getting the dot product of values in df1 with coefficients in df2? The output would look like:

Date       entry  result
2017-04-12    A     54
2017-04-12    B     58
2017-04-12    C     62
2017-04-13    A     146
2017-04-13    B     152
2017-04-13    D     158

I know that looping over dates works.


Solution

  • Use DataFrame.mul with MultiIndex in df1 with transpose df2, then sum per rows and convert MultiIndex Series by Series.reset_index to DataFrame:

    df = (df1.set_index(['Date','entry'])
             .mul(df2.T, level=0)
             .sum(axis=1)
             .reset_index(name='result'))
    print (df)
             Date entry  result
    0  2017-04-12     A      54
    1  2017-04-12     B      58
    2  2017-04-12     C      62
    3  2017-04-13     A     146
    4  2017-04-13     B     152
    5  2017-04-13     D     158