I just stumbled upon the same problem. It seems that pandas wants the column and row index to be aligned to multiply by elements, so you can just rename with your display during multiplication:
>>> df1 = pd.DataFrame([[1,2,3]], index=['1', '2', '3'], columns=['a', 'b', 'c']) >>> df2 = pd.DataFrame([[4,5,6]], index=['1', '2', '3'], columns=['d', 'e', 'f']) >>> df1 abc 1 1 2 3 2 1 2 3 3 1 2 3 >>> df2 def 1 4 5 6 2 4 5 6 3 4 5 6 >>> mapping = {'a' : 'e', 'b' : 'd', 'c' : 'f'} >>> df1.rename(columns=mapping) * df2 def 1 8 5 18 2 8 5 18 3 8 5 18
If you need a โnaturalโ column order, you can create a mapping on the fly, for example:
>>> df1 * df2.rename(columns=dict(zip(df2.columns, df1.columns)))
for example, to make the โinternal Frobenius productโ of two matrices, you could do:
>>> (df1 * df2.rename(columns=dict(zip(df2.columns, df1.columns)))).sum().sum() 96
source share