Home > database >  How to perform calculations on dictionaries like data frames
How to perform calculations on dictionaries like data frames

Time:07-23

I'm using binance websocket api to get bids and asks from an orderbook as a json and would like to perform some calculations. The dictionary looks like this:{'bids': [['20768.16000000', '0.07273000'], [....]] 'asks': [['20770.77000000', '0.00096000'], [...]]} Given a dictionary of tuples, is there a way to multiply the values inside the tuples and add them all up?

For example, Currently I'm using pandas but I'd guess it's more efficient without the need of an extra data frame:

    bids = pd.DataFrame(response['bids_size'])
    asks = pd.DataFrame(response['asks_size'])
    df = pd.merge(bids, asks, left_index=True, right_index=True)
    df = df.apply(pd.to_numeric)

        0_x      1_x       0_y      1_y
   0  20768.16  0.07273  20770.77  0.00096  
   1  20767.49  0.01352  20771.15  0.03400   
   2  20767.38  0.04119  20771.19  0.05400   
   3  20767.28  0.00055  20771.20  0.12411   
   4  20766.83  0.04000  20771.22  0.00962


    

    df['bids'] = df['0_x'] * df['1_x']
    df['asks'] = df['0_y'] * df['1_y']

gives:

    0_x      1_x       0_y      1_y       bids_size     asks_size
0  20768.16  0.07273  20770.77  0.00096  1510.468277    19.939939
1  20767.49  0.01352  20771.15  0.03400   280.776465   706.219100
2  20767.38  0.04119  20771.19  0.05400   855.408382  1121.644260
3  20767.28  0.00055  20771.20  0.12411    11.422004  2577.913632
4  20766.83  0.04000  20771.22  0.00962   830.673200   199.819136     

does anyone know if this can be done directly from the dictionary? thanks

Edit: a dictionary response would look as such:

{'bids': [['23767.25000000', '0.00100000'], ['23766.82000000', 
'0.02102000'], ['23766.81000000', 
'0.01081000'], ['23766.79000000', '0.01758000'], ['23766.75000000', 
'0.00051000'], ['23766.10000000', '0.00168000'], ['23766.01000000', 
'0.00400000'], ['23765.95000000', '0.00092000'], ['23765.40000000', 
'0.13471000'], ['23765.06000000', '0.04420000']], 'asks': 
[['23768.53000000', '0.00195000'], ['23768.57000000', '0.10468000'], 
['23768.68000000', '0.13800000'], ['23768.93000000', '0.00166000'], 
['23769.02000000', '0.01709000'], ['23769.38000000', '0.01799000'], 
['23770.67000000', '0.00515000'], ['23771.58000000', '0.00696000'], 
['23771.75000000', '0.05799000'], ['23771.76000000', '0.13800000']]}

CodePudding user response:

is this what you are looking for

df = read_csv(path_to_file)

def mult(x,y):return x*y

result = list(map(mult,df['0_x'],df['1_x']))

result2 = list(map(mult,df['0_y'],df['1_y']))

the output

result [431302555.11840004, 0.0009833096000000001, 431432779.28550005, 3.2640000000000006e-05]

result2 [11, 16, 330, 32]

Edit:

import numpy as np

df = {
  'bids': [['5', '4'], 
           ['2', '1'],
           ['2', '15']
           
          ], 
  'asks': [['3', '12'],
           ['11', '9'],
           ['7', '0']
           
          ]
  }

def mult(*args):
   args = tuple(j for j in args[0])
   result = 1   
   for num in args:
      result = list(np.multiply(result,num))
   return list(result)
                 
result = []                             
for item in df:
    values = df[item]
    for i,value in enumerate(values):
        values[i] = list(map(float,value))
    result.append(mult(df[item]))
 

edit 2

df = {
'bids': [['5', '4'], 
       ['2', '1'],
       ['2', '15']
       
      ], 
'asks': [['3', '12'],
       ['11', '9'],
       ['7', '0']
       
      ]
}

def mult(*args):
  result2 = []
  args = tuple(j for j in args[0])
 
  for num in args:
      result = 1 
      for item in num:
          result *=item 
      result2.append(result)
  return list(result2)
              
result = []                             
for item in df:
    values = df[item]
    for i,value in enumerate(values):
        values[i] = list(map(float,value))
    result.append(mult(df[item]))

output:

[[20.0, 2.0, 30.0], [36.0, 99.0, 0.0]]
  • Related