在 Python 中按共同日期加入时间序列(数据框和系列/列表问题)

菜鸟在这里。请原谅我仍在学习的格式。我正在尝试创建一个包含三列的时间序列(我认为是数据框?)。一个是日期列,下一个是库存列,最后一个是价格列。


我已经提取了两个单独的系列(日期和库存;日期和价格),我想合并这两个系列,这样我就可以看到三列而不是两组两列。这是我的代码。


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")

因为显然我不知道列表和系列之间的区别。


如何按日期列合并这些列表?非常感谢您的帮助。(也请随意告诉我为什么我在这篇文章中正确格式化代码很糟糕。)


白衣染霜花
浏览 175回答 1
1回答

慕标5832272

如果您想在其余代码中将它们作为 DataFrame 进行操作,您可以将它们转换ngstor为ngpxDataFrame,如下所示:import pandas as pd# I create two lists that look like yoursngstor = [[1,2], ["2020-04-03", "2020-05-07"]]ngpx = [[3,4] , ["2020-04-03", "2020-05-07"]]# I transform them to DataFramesngstor = pd.DataFrame({"value1": ngstor[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"date_col": ngstor[1]})ngpx = pd.DataFrame({"value2": ngpx[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"date_col": ngpx[1]})然后您可以使用pandas.merge或pandas.concat:# merge optionjoined_framed = pd.merge(ngstor, ngpx, on="date_col",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; how="outer")# concat optionngstor = ngstor.set_index("date_col")ngpx = ngpx.set_index("date_col")joined_framed = pd.concat([ngstor, ngpx], axis=1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join="outer").reset_index()结果将是:&nbsp; &nbsp; date_col&nbsp; value1&nbsp; value20&nbsp; 2020-04-03&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;31&nbsp; 2020-05-07&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp;4
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python