我试图从binance(交换,API)中获取一些历史数据,并且我正在使用他们的模块。我可以获取数据,但无论何时使用下面的代码,它都会创建0和#39;关闭'和'卷':
ticker = 'NANOETH'
interv = "1d"
period = "1M"
rest_client.klines(ticker, period)
klines = rest_client.klines(symbol=ticker, interval=interv)
df=[]
for kline in klines:
df=[kline.open_time, kline.open, kline.high,kline.low, kline.close, kline.volume]
df = pd.DataFrame([df], columns = ["Open Time","Close Time","Open","High","Low","Close", "Volume"])
print (df)
输出结果为:
Open Time Close Time Open High Low \
0 1517529600000 1517615999999 0.02010000 0.02998700 0.01764600
Close Volume
0 0.01841200 448361.60000000
Open Time Close Time Open High Low \
0 1517616000000 1517702399999 0.01848800 0.01890000 0.01600300
Close Volume
0 0.01700900 410642.17000000
Open Time Close Time Open High Low \
0 1517529600000 1517615999999 0.02010000 0.02998700 0.01764600
Close Volume
0 0.01841200 448361.60000000
我希望它看起来像:
Open Time Close Time Open High Low Close Volume
1517529600000 1517615999999 0.02010000 0.02998700 0.01764600 0.01841200 448361.60000000
1517616000000 1517702399999 0.01848800 0.01890000 0.01600300 0.01700900 410642.17000000
1517529600000 1517615999999 0.02010000 0.02998700 0.01764600 0.01841200 448361.60000000
并且还想索引第一个日期并将其转换为实际的日期时间。
非常感谢
答案 0 :(得分:0)
通过列表理解创建列表列表。然后输入pd.DataFrame
作为最后一步。
lst = [[k.open_time, k.open, k.high, k.low, k.close, k.volume] for kline in klines]
df = pd.DataFrame(lst, columns=['Open Time', 'Close Time', 'Open',
'High', 'Low', 'Close', 'Volume'])
答案 1 :(得分:0)
我不确定type
对象klines
是什么,但我假设它是pd.DataFrame
。你可以在将来像这样对它进行子集化:
# Get the indicies you want
idx_want = ["open_time", "open", "high", "low", "close", "volume"]
# Subset the pd.DataFrame (asseuming klines is a dataframe
df_subset = klines.loc[:,idx_want]
# If klines is a list of pd.Series then do `klines = pd.DataFrame(klines)`
# Relabel Columns
df_subset.columns = ['Open Time', 'Close Time', 'Open', 'High', 'Low', 'Close', 'Volume']