Home > Mobile >  Python pandas Dataframe
Python pandas Dataframe

Time:10-15

I'm really new to python, just playing around. I have script that gets

------- received message ------- {"e":"24hrMiniTicker","E":1634212481158,"s":"ADAUSDT","c":"2.19200000","o":"2.12100000","h":"2.20000000","l":"2.10000000","v":"119875239.00000000","q":"259043876.72690000"}

from Binance stream. I'm able to print out this line in terminal and add it to an array, but I want to run this script in the background and have another script fill a pandas dataframe with that data.

1st Script:

import websocket
import numpy as np
import config
import pandas as pd
from binance.client import Client
from binance.enums import *
message_store = np.array
SYMBOL = "adausdt"
SOCKET = "wss://stream.binance.com:9443/ws/"   SYMBOL   "@miniTicker"
client = Client(config.API_KEY, config.API_SECRET, tld='us')
def on_open(ws):
    print('opened connection')


def on_close(ws):
    print('closed connection')


def on_message(ws, message):
    print('------- received message -------')
    print(message)
    message_store.append(message)
    #x = len(message_store)
    
    #print(x)

ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)  
ws.run_forever()

2nd Script:

from getprice import message_store
import pandas as pd

dataframe = pd.DataFrame(data=message_store) 
print(dataframe)

ValueError: DataFrame constructor not properly called!

Any ideas how to do this the correct way?

CodePudding user response:

First, you are constructing the message_store wrong, you are assigning the consructor to it instead of an instance of np.array. second, in this case you should use a regular list instead of np.array because:

  1. you don't know the size of the final array when you construct it
  2. The array contain an object and not any kind of numeric data

so you should use:

message_store = []

instead of

message_store = np.array

Small example:

import pandas as pd
lst = []
lst.append({"e": "24hrMiniTicker", "E": 1634212481158, "s": "ADAUSDT", "c": "2.19200000", "o": "2.12100000", "h": "2.20000000", "l": "2.10000000",
            "v": "119875239.00000000", "q": "259043876.72690000"})
lst.append({"e": "24hrMiniTicker", "E": 1634212481158, "s": "ADAUSDT", "c": "2.19200000", "o": "2.12100000", "h": "2.20000000", "l": "2.10000000",
            "v": "119875239.00000000", "q": "259043876.72690000"})
df = pd.DataFrame(lst)
print(df.head(5))
print(df["e"])
  • Related