cmac
cmac

Reputation: 13

Need help multiplying two Pandas dataframes

I've got two dataframes of equal dimensions and I simply want to multiply them to get a new dataframe containing the product of their elements. However, instead I'm getting 6 columns of nothing but "NaN"s...

These are the two dataframes:

>>> period_subset_data
               SP500    NASDAQ      US20
1999-08-02  0.004698 -0.005727  0.000751
1999-08-03 -0.005810 -0.007322 -0.002250
...              ...       ...       ...
2015-04-22  0.002943  0.002129 -0.011747

[4103 rows x 3 columns]

>>> facmat
               SP500    NASDAQ      US20
1999-08-02  0.979367  0.670188  1.696232
1999-08-03  0.979367  0.670188  1.696232
...              ...       ...       ...
2015-04-21  0.979367  0.670188  1.696232
2015-04-22  0.979367  0.670188  1.696232

[4103 rows x 3 columns]

When I try to multiply them, this is what I get: norm_returns=period_subset_data*facmat

>>> norm_returns
            SP500  NASDAQ  US20  (SP500,)  (NASDAQ,)  (US20,)
1999-08-02    NaN     NaN   NaN       NaN        NaN      NaN
1999-08-03    NaN     NaN   NaN       NaN        NaN      NaN
...           ...     ...   ...       ...        ...      ...
2015-04-21    NaN     NaN   NaN       NaN        NaN      NaN
2015-04-22    NaN     NaN   NaN       NaN        NaN      NaN

[4103 rows x 6 columns]

I'm running code written by someone else that apparently worked for them, so I'm struggling to understand why it isn't working for me...

Upvotes: 0

Views: 95

Answers (2)

Yuca
Yuca

Reputation: 6091

it's a very common problem, the easiest way around it is to do:

norm_returns = period_subset_data*facmat.values

However, I tried doing just

norm_returns = period_subset_data*facmat

and it worked just fine with the sample data you gave us.

Upvotes: 0

Naveen
Naveen

Reputation: 1210

You can try the following as you need to multiply based on the index. I found this answer from here.

pd.DataFrame(df.values*df2.values, columns=df.columns, index=df.index)

Upvotes: 1

Related Questions