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

时间:2016-01-29 15:40:07

标签: python python-3.x pandas dataframe datareader

我制作了这段代码,最终得到了一个包含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') 

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要在axis=1电话中添加concat

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

这能否为您提供所追求的目标?

答案 1 :(得分:0)

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

function AppViewModel(listValues) {
    this.tempArray = [];

    this.listValues =  ["Arnold","Bnolg","Cold"];
    this.myArray = ko.observableArray();
    this.searchValue = ko.observable();
    this.myArray=this.listValues;
    var self =this;
    this.check = function(){

        self.myArray=[];
        for(var i=0;i<this.listValues.length;i++){
            var kill = this.searchValue();
            if(this.listValues[i].indexOf(kill) > -1){
                this.tempArray.push(this.listValues[i]);
            }
        }
        this.myArray=this.tempArray;
    };
}
ko.applyBindings(new AppViewModel);

如果你想沿着列轴连接,请将# add a symbol column r[xrt] = xrt 传递给axis='columns'

concat()