无法在 python pandas 数据框中附加嵌套的 JSON 值
Can't append nested JSON value in python pandas dataframe
我尝试将 JSON 数据中的一个值附加到数据帧并打印它。
有效的是打印 close
,或者附加并打印整个 JSON 块 json_message
。
不幸的是,如果我用 json_message['k']['c']
尝试它,它不会打印任何东西
我如何才能仅访问和打印嵌套的 'c' 值?
import websocket, json, pprint
import numpy as np
import pandas as pd
SOCKET = "wss://stream.binance.com:9443/ws/ethusdt@kline_1m"
df = pd.DataFrame(columns=['1'])
def on_open(ws):
print('opened connection')
def on_close(ws):
print('closed connection')
def on_message(ws, message):
global df
json_message = json.loads(message)
pprint.pprint(json_message)
close = float(json_message['k']['c'])
df = df.append(close, ignore_index=True)
is_bar_closed = json_message['k']['x']
if is_bar_closed == False:
print(close)
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
输出:
{'E': 1616880759079,
'e': 'kline',
'k': {'B': '0',
'L': 339352101,
'Q': '89163.86464830',
'T': 1616880779999,
'V': '51.79165000',
'c': '1721.18000000',
'f': 339351937,
'h': '1721.69000000',
'i': '1m',
'l': '1721.00000000',
'n': 165,
'o': '1721.66000000',
'q': '154581.23992910',
's': 'ETHUSDT',
't': 1616880720000,
'v': '89.79198000',
'x': False},
's': 'ETHUSDT'}
期望的输出:
{'E': 1616880831330,
'e': 'kline',
'k': {'B': '0',
'L': 339352406,
'Q': '134124.45737740',
'T': 1616880839999,
'V': '77.94314000',
'c': '1720.65000000',
'f': 339352213,
'h': '1720.82000000',
'i': '1m',
'l': '1720.59000000',
'n': 194,
'o': '1720.81000000',
'q': '177486.68404770',
's': 'ETHUSDT',
't': 1616880780000,
'v': '103.14269000',
'x': False},
's': 'ETHUSDT'}
1720.65
您的代码存在问题,您正在尝试将 float
附加到 dataframe
,这不是受支持的操作:df = df.append(close, ignore_index=True)
这里close
必须是DataFrame or Series/dict-like object, or list of these.
所以将 close
转换为 dict
应该可以解决问题:
row = {'1': close}
现在我们可以将此 row
附加到 df
(打印 df
以查看它是否有效):
def on_message(ws, message):
global df
json_message = json.loads(message)
pprint.pprint(json_message)
close = float(json_message['k']['c'])
row = {'1': close}
df = df.append(row, ignore_index=True)
print(df)
is_bar_closed = json_message['k']['x']
if is_bar_closed == False:
print(close)
我尝试将 JSON 数据中的一个值附加到数据帧并打印它。
有效的是打印 close
,或者附加并打印整个 JSON 块 json_message
。
不幸的是,如果我用 json_message['k']['c']
尝试它,它不会打印任何东西
我如何才能仅访问和打印嵌套的 'c' 值?
import websocket, json, pprint
import numpy as np
import pandas as pd
SOCKET = "wss://stream.binance.com:9443/ws/ethusdt@kline_1m"
df = pd.DataFrame(columns=['1'])
def on_open(ws):
print('opened connection')
def on_close(ws):
print('closed connection')
def on_message(ws, message):
global df
json_message = json.loads(message)
pprint.pprint(json_message)
close = float(json_message['k']['c'])
df = df.append(close, ignore_index=True)
is_bar_closed = json_message['k']['x']
if is_bar_closed == False:
print(close)
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
输出:
{'E': 1616880759079,
'e': 'kline',
'k': {'B': '0',
'L': 339352101,
'Q': '89163.86464830',
'T': 1616880779999,
'V': '51.79165000',
'c': '1721.18000000',
'f': 339351937,
'h': '1721.69000000',
'i': '1m',
'l': '1721.00000000',
'n': 165,
'o': '1721.66000000',
'q': '154581.23992910',
's': 'ETHUSDT',
't': 1616880720000,
'v': '89.79198000',
'x': False},
's': 'ETHUSDT'}
期望的输出:
{'E': 1616880831330,
'e': 'kline',
'k': {'B': '0',
'L': 339352406,
'Q': '134124.45737740',
'T': 1616880839999,
'V': '77.94314000',
'c': '1720.65000000',
'f': 339352213,
'h': '1720.82000000',
'i': '1m',
'l': '1720.59000000',
'n': 194,
'o': '1720.81000000',
'q': '177486.68404770',
's': 'ETHUSDT',
't': 1616880780000,
'v': '103.14269000',
'x': False},
's': 'ETHUSDT'}
1720.65
您的代码存在问题,您正在尝试将 float
附加到 dataframe
,这不是受支持的操作:df = df.append(close, ignore_index=True)
这里close
必须是DataFrame or Series/dict-like object, or list of these.
所以将 close
转换为 dict
应该可以解决问题:
row = {'1': close}
现在我们可以将此 row
附加到 df
(打印 df
以查看它是否有效):
def on_message(ws, message):
global df
json_message = json.loads(message)
pprint.pprint(json_message)
close = float(json_message['k']['c'])
row = {'1': close}
df = df.append(row, ignore_index=True)
print(df)
is_bar_closed = json_message['k']['x']
if is_bar_closed == False:
print(close)