【发布时间】:2018-01-10 12:35:43
【问题描述】:
我正在尝试创建一个循环,该循环将为每个代码返回, 1. 不同的数据框(按股票代码的名称) 2.将时间列转换为“正常”日期 3. 它(新时间)将用作该数据帧的索引。
如果我为每个代码运行它,它就可以正常工作。 感谢您的帮助!
import requests
import pandas as pd
desired_width = 320
pd.set_option('display.width', desired_width)
data = pd.DataFrame()
tickers = ['BTC', 'ETH', 'XRP'] # pools of tickers to get
for t in tickers: # a loop to get data ticker by ticker
url = 'https://min-api.cryptocompare.com/data/histoday' + \
'?fsym=' + \
t +\
'&tsym=USD' + \
'&limit=600000000000' + \
'&aggregate=1' + \
'&e=CCCAGG'
response = requests.get(url)
data[t] = response.json()['Data']
#the following 2 lines I failed to execute
#data[t]['time'] = pd.to_datetime(data[t]['time'], unit='s')
#data[t].index = data[t]['time']
print("downloading data for: " + t)
print("data for:" + t, data.head(5))
我的结果是所有三个代码的一个数据框:
数据:XRP BTC
ETH XRP 0 {'时间': 1342742400,“关闭”:8.52,“高”:8 .... {“时间”:1342742400, 'close': 0, 'high': 0, 'l... {'time': 1342742400, 'close': 0, 'high': 0, 'l... 1 {'time': 1342828800, 'close': 8.85, 'high': 9.... {'time': 1342828800, 'close': 0, 'high': 0, 'l... {'time': 1342828800,'关闭':0,'高':0,'l ... 2 {'时间':1342915200, 'close': 8.41, 'high': 8.... {'time': 1342915200, 'close': 0, 'high': 0, 'l... {'time': 1342915200, 'close': 0, 'high': 0, 'l... 3 {'time': 1343001600, 'close': 8.45, 'high': 9.... {'time': 1343001600,'关闭':0,'高':0,'l ... {'时间':1343001600, 'close': 0, 'high': 0, 'l... 4 {'time': 1343088000, 'close': 8.6, 'high': 8.8... {'time': 1343088000, 'close': 0, 'high': 0, 'l... {'time': 1343088000, 'close': 0, 'high': 0, 'l...
我在 Windows 10 上使用带有 pycharm + anconda 的 python 3.6
【问题讨论】:
标签: python pandas python-requests