Skip to content
Advertisement

Websocket Json Data to DataFrame

I am learning how to work with APIs and web sockets in finance. My goal for this code is to access data and create a DataFrame with only columns (index, ask, bid & quote) I have tried appending values to the DataFrame but it creates a new DataFrame every time I receive a message similar to the df = new_df.loc[0] = data

Output of the current code

JavaScript

Dictionary after loading json

JavaScript

Desired output

index, ask, bid, quote as columns

Append values as rows every time we get a new message or tick.

JavaScript

Advertisement

Answer

You should create empty DataFrame at start as global variable and later use .append() to add new rows to this dataframe.

Minimal working example (but without API)

JavaScript

Result:

JavaScript

EDIT:

Because .append() is deprecated and pandas suggests to use concat():

JavaScript

Version with websocket – but I can’t test it

EDIT: Original question was using ws.binaryws.com but author changed it into websocket but this can be useless for other users – so I will keep original ws.binaryws.com in my code.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement