首页 > 解决方案 > 修订 - 如何从 pandas 中检索数据

问题描述

我正在尝试从以下结果中检索最新数据。

我正在运行的代码是:

import datetime
from Historic_Crypto import HistoricalData

# Time Setup
now_bsc = datetime.datetime.now()
now = (now_bsc.strftime("%Y-%m-%d-%H-%M"))

start_bsc = (datetime.datetime.now() - datetime.timedelta(minutes=10))
start = (start_bsc.strftime("%Y-%m-%d-%H-%M"))
"""print(start)
print(now)"""

# Pull OHLC Crypto
new = HistoricalData('BTC-USD', 60, start, now).retrieve_data()

print(new)

这是结果

Checking input parameters are in the correct format...
Formatting Dates...
Checking if ticker supplied is available on the CoinBase Pro API...
Connected to the CoinBase Pro API...
Ticker 'BTC-USD' found at the CoinBase Pro API, continuing to extraction...
Data Extracted from API...
                          low      high      open     close     volume
time                                                                  
2021-05-12 21:18:00  54577.75  54641.08  54637.08  54582.91  14.041055
2021-05-12 21:19:00  54572.71  54610.19  54582.91  54599.86   3.327732
2021-05-12 21:20:00  54599.86  54691.49  54599.86  54678.31  17.552511
2021-05-12 21:21:00  54677.93  54725.75  54677.93  54711.22   5.963198
2021-05-12 21:22:00  54703.62  54756.39  54710.24  54753.88  11.564766
2021-05-12 21:23:00  54750.42  54800.47  54753.88  54768.64  13.601058
2021-05-12 21:24:00  54707.11  54768.64  54768.64  54738.99  11.131357
2021-05-12 21:25:00  54499.99  54739.49  54738.89  54669.53  37.366287
2021-05-12 21:26:00  54665.96  54748.10  54665.97  54743.89   7.932319
2021-05-12 21:27:00  54719.23  54752.19  54736.09  54734.70   9.654189
2021-05-12 21:28:00  54719.97  54749.00  54725.91  54748.99   6.386556

Process finished with exit code 0

如何检索 2021-05-12 21:28:000 的数据“低”和“高”?

标签: pythonpandas

解决方案


此 API 返回 pandas Dataframe 对象。

您可以在此处使用 pandas 数据帧过滤器选项。

例如:

data = new.loc[(new['time'] == '2021-05-12 21:28:000')]
low = data['low']
high = data['high']

推荐阅读