0

I am trying to make a simple websocket + pandas implementation, my problem is every time stream runs, whole pandas datafram gets updated. I want to just add one row that has been added to dataframe.\

import json
import pandas as pd
import websocket

df = pd.DataFrame(columns=['foreignNotional', 'grossValue', 'homeNotional', 'price', 'side',
                          'size', 'symbol', 'tickDirection', 'timestamp', 'trdMatchID'])

def on_message(ws, message):
    msg = json.loads(message)
    msg = msg['data']
    global df
    df=df.append(msg, ignore_index=True)
    df.head()
    
def on_close(ws):
    print("## CLOSED ##")

def on_error(ws, error):
    print(error)
    
def on_open(ws):
    return

if __name__ == "__main__":
    ws = websocket.WebSocketApp("wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD",
                                on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close)
    ws.run_forever()
shyamsantoki
  • 115
  • 1
  • 9
  • 1
    pandas doesn't support to add/remove rows to/from a DataFrame – Michael Butscher Jul 18 '20 at 16:41
  • 2
    Consider just appending msg to a list, which will be a list of dictionaries, then on close or when you need work with the data, convert that list to df, as per [this answer](https://stackoverflow.com/a/62734983/7891326) – Bertil Johannes Ipsen Jul 18 '20 at 17:08

0 Answers0