python-3.xpandasdataframealgebra

How can I multiply two dataframes in Python as I would in algebra (row x column)?


I would like to multiply two dataframes as a would do in algebra (row x column). (i,:)*(:,i).

How can I do this with python (I really can't beleave that in 2 h I haven't had a solution ... algebra library must be included in/with python)?

This is the matrix:

yantigua         open      high       low     close  tick_volume    spread
0   0.435509  0.420361  0.425533  0.403871     0.183026  0.023377
1   0.433416  0.418509  0.425276  0.402005     0.182564  0.023694
2   0.433800  0.418664  0.422303  0.401137     0.182140  0.023342
3   0.434401  0.418587  0.420415  0.400299     0.181736  0.022967
4   0.434766  0.418440  0.421036  0.401038     0.181914  0.022970
..       ...       ...       ...       ...          ...       ...
93  0.381397  0.358522  0.362086  0.328688     0.164687  0.023678
94  0.381438  0.358380  0.362260  0.328865     0.165061  0.023799
95  0.382542  0.359656  0.364354  0.331056     0.165959  0.024164
96  0.385499  0.362927  0.367922  0.335734     0.167352  0.024522
97  0.389977  0.368252  0.375718  0.343705     0.169503  0.025446

and this is the vector:

standardesv                        0
open            0.021056
high            0.020998
low             0.021040
close           0.021049
tick_volume  9505.609835
spread          9.902313

I've tried with this:

for i in range(largo):
        multipli[i,:] = standardesv[i,0]* yantigua[:,i] 

and I get this error:

pandas.errors.InvalidIndexError: (0, 0)

and with this:

multipli=yantigua.multiply(standardesv)

this is the result (useless):

multipli               0  close  high  low  open  spread  tick_volume
0           NaN    NaN   NaN  NaN   NaN     NaN          NaN
1           NaN    NaN   NaN  NaN   NaN     NaN          NaN
2           NaN    NaN   NaN  NaN   NaN     NaN          NaN
3           NaN    NaN   NaN  NaN   NaN     NaN          NaN
4           NaN    NaN   NaN  NaN   NaN     NaN          NaN
...          ..    ...   ...  ...   ...     ...          ...
high        NaN    NaN   NaN  NaN   NaN     NaN          NaN
low         NaN    NaN   NaN  NaN   NaN     NaN          NaN
open        NaN    NaN   NaN  NaN   NaN     NaN          NaN
spread      NaN    NaN   NaN  NaN   NaN     NaN          NaN
tick_volume NaN    NaN   NaN  NaN   NaN     NaN          NaN

but doesn't seem to work, specially the pd mul, where it merge the row

I've also tried with this:

    multipli=np.dot(np.array(standardesv),np.array(yantigua))

with this error:

    ValueError: shapes (6,1) and (98,6) not aligned: 1 (dim 1) != 98 (dim 0)

and if I transpose the vector, it gives this:

ValueError: shapes (1,6) and (98,6) not aligned: 6 (dim 1) != 98 (dim 0)

A clear expected output, should be like this one:

multipli               0  close  high  low  open  spread  tick_volume
0           0.021056*0.435509=0.0091700    0.020998*0.420361=0.0088267  and so on ...          
1           0.021056*0.433416=  0.009126  
2           and so on
3             ...      
...          ..    ...   ...  ...   ...     ...          ...
93         ...
94         ...
95         ...
96         ...
97         ...

Please help!, how can I make this?, thanks ;)

edited:

    print(yantigua.sample(10).to_dict())

{'open': {2: 0.4280005395412445, 27: 0.5021759867668152, 96: 0.3646944463253021, 54: 0.47579672932624817, 22: 0.48902419209480286, 23: 0.4927292764186859, 44: 0.47958144545555115, 43: 0.4764374792575836, 94: 0.35704782605171204, 0: 0.43094953894615173}, 'high': {2: 0.4308417737483978, 27: 0.5052538514137268, 96: 0.36964157223701477, 54: 0.4819599986076355, 22: 0.49243873357772827, 23: 0.4958927035331726, 44: 0.48174402117729187, 43: 0.47958746552467346, 94: 0.3601619303226471, 0: 0.43414050340652466}, 'low': {2: 0.4293111562728882, 27: 0.5040982961654663, 96: 0.36629873514175415, 54: 0.48055922985076904, 22: 0.49039655923843384, 23: 0.49451470375061035, 44: 0.48186397552490234, 43: 0.4795111417770386, 94: 0.358450710773468, 0: 0.4328794479370117}, 'close': {2: 0.4419543445110321, 27: 0.5150494575500488, 96: 0.3805628716945648, 54: 0.4903129041194916, 22: 0.5038134455680847, 23: 0.5072773098945618, 44: 0.493206262588501, 43: 0.4902387261390686, 94: 0.37201452255249023, 0: 0.44500732421875}, 'tick_volume': {2: 0.2304738461971283, 27: 0.22799265384674072, 96: 0.23698511719703674, 54: 0.22721892595291138, 22: 0.22034436464309692, 23: 0.22251935303211212, 44: 0.22245052456855774, 43: 0.22552646696567535, 94: 0.23688112199306488, 0: 0.2327653169631958}, 'spread': {2: 0.028131725266575813, 27: 0.026681670919060707, 96: 0.026082312688231468, 54: 0.027271414175629616, 22: 0.028713377192616463, 23: 0.028218993917107582, 44: 0.027661295607686043, 43: 0.027516597881913185, 94: 0.02764700911939144, 0: 0.026977157220244408}}

    print(standardesv.to_dict())

{'open': {0: 0.02105550201530985}, 'high': {0: 0.02099772512759739}, 'low': {0: 0.021040503194702517}, 'close': {0: 0.021048686752239296}, 'tick_volume': {0: 9503.151409117114}, 'spread': {0: 9.902313457561917}}

Edited2:

When doing this:

    largo=len(yantigua)
    for i in range(largo):
        multipli = standardesv[i,:]* yantigua[:,i] 

it gives this error:

pandas.errors.InvalidIndexError: (0, slice(None, None, None))

Solution

  • IIUC, you can use :

    multipli = yantigua.mul(standardesv.squeeze(), axis=1)
    

    Output :

    print(multipli)
    ā€‹
        open  high  low  close  tick_volume  spread
    2   0.01  0.01 0.01   0.01      2190.23    0.28
    27  0.01  0.01 0.01   0.01      2166.65    0.26
    96  0.01  0.01 0.01   0.01      2252.11    0.26
    54  0.01  0.01 0.01   0.01      2159.30    0.27
    22  0.01  0.01 0.01   0.01      2093.97    0.28
    23  0.01  0.01 0.01   0.01      2114.64    0.28
    44  0.01  0.01 0.01   0.01      2113.98    0.27
    43  0.01  0.01 0.01   0.01      2143.21    0.27
    94  0.01  0.01 0.01   0.01      2251.12    0.27
    0   0.01  0.01 0.01   0.01      2212.00    0.27