将 2 个数据帧的列表合并为一个具有 2 列的 df

Merging list of 2 dataframes into one df with 2 columns

我制作的代码以 2 个数据帧的列表和一个内容错误的数据帧结尾。

创建的 DF 如下所示:

DATE    DEXDNUS DEXUSEU
2016-01-20  DEXDNUS nan
2016-01-21  DEXDNUS nan 
2016-01-22  DEXDNUS nan
2014-12-04  nan DEXUSEU
2014-12-05  nan DEXUSEU
2014-12-08  nan DEXUSEU

但我需要的是实际的每日汇率,而不仅仅是货币符号...

像这样

DATE    DEXDNUS DEXUSEU
2014-12-04  6.78    1.24
2014-12-05  6.86    1.23
2014-12-08  6.81    1.27

我该怎么做?

import pandas as pd
import pandas.io.data as web
import datetime

xratelist = ['DEXDNUS', 'DEXUSEU']
xrts = []

def xRateList_pd(xratelist, modus='trading',start=datetime.datetime(2000,1,1),end=pd.Timestamp.utcnow()):    

    years = 1.2
    days = int(252 * years)  # ant. arb. dage pr år = 252

    if modus == 'trading':
        end     = pd.Timestamp.utcnow()
        start   = end - days * pd.tseries.offsets.BDay()

    print('Fetching xratelist from Fred: ', xratelist)
    for xrt in xratelist:
        r = web.DataReader(xrt, 'fred',
                       start = start, end = end)
        # add a symbol column
        r[xrt] = xrt
        xrts.append(r)
    # concatenate all the dfs into one
    df_xrates = pd.concat(xrts)

    return df_xrates

if __name__ == '__main__':
    df_xrates = xRateList_pd(xratelist, modus='trading') 

如果我理解正确,您只需将 axis=1 添加到您的 concat 调用中:

df_xrates = pd.concat(xrts,axis=1)

这是否给了你想要的东西?

如果不需要符号列,请删除此部分:

# add a symbol column
r[xrt] = xrt

如果要沿列轴连接,则将 axis='columns' 传递给 concat()

df_xrates = pd.concat(xrts, axis='columns')