菜鸟在这里。请原谅我仍在学习的格式。我正在尝试创建一个包含三列的时间序列(我认为是数据框?)。一个是日期列,下一个是库存列,最后一个是价格列。
我已经提取了两个单独的系列(日期和库存;日期和价格),我想合并这两个系列,这样我就可以看到三列而不是两组两列。这是我的代码。
import json
import numpy as np
import pandas as pd
from urllib.error import URLError, HTTPError
from urllib.request import urlopen
class EIAgov(object):
def __init__(self, token, series):
'''
Purpose:
Initialise the EIAgov class by requesting:
- EIA token
- id code(s) of the series to be downloaded
Parameters:
- token: string
- series: string or list of strings
'''
self.token = token
self.series = series
def __repr__(self):
return str(self.series)
def Raw(self, ser):
# Construct url
url = 'http://api.eia.gov/series/?api_key=' + self.token + '&series_id=' + ser.upper()
try:
# URL request, URL opener, read content
response = urlopen(url);
raw_byte = response.read()
raw_string = str(raw_byte, 'utf-8-sig')
jso = json.loads(raw_string)
return jso
except HTTPError as e:
print('HTTP error type.')
print('Error code: ', e.code)
except URLError as e:
print('URL type error.')
print('Reason: ', e.reason)
请注意,“mytoken”需要替换为 eia.gov API 密钥。我可以让它成功地创建两个列表的输出......但是为了合并列表,我试图在最后添加这个:
joined_frame = pd.concat([ngstor, ngpx], axis = 1, sort=False)
print(joined_frame.GetData())
但我得到一个错误
("TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid")
因为显然我不知道列表和系列之间的区别。
如何按日期列合并这些列表?非常感谢您的帮助。(也请随意告诉我为什么我在这篇文章中正确格式化代码很糟糕。)
慕标5832272
相关分类