1

当我应该阅读如何推送和推送数据帧数据时,我真的很遗憾去酒吧:

给定两个形状相似的数据框,我该如何组合它们?

> print aae.head(), aar.head()

           symbol      open      high       low     close  volume
date                                                             
1994-02-21    AAE  22.83890  22.83890  20.47626  20.47626    4368
1994-02-22    AAE  20.47626  20.47626  19.68871  19.68871    2120
1994-02-23    AAE  20.47626  22.05135  20.47626  22.05135    7961
1994-02-24    AAE  22.05135  23.62645  22.05135  22.83890    2768
1994-02-25    AAE  22.83890  22.83890  21.26381  21.26381    1778
         symbol      open      high       low     close  volume
date                                                             
1992-01-02  AAR  0.11657  0.11657  0.11657  0.11657   61767
1992-01-03  AAR  0.11657  0.11657  0.11657  0.11657   10294
1992-01-07  AAR  0.12628  0.12628  0.12628  0.12628  102944
1992-01-09  AAR  0.12628  0.12628  0.12628  0.12628  102944
1992-01-10  AAR  0.12628  0.13600  0.12628  0.13600   18530

这样它会丢弃两个系列中都没有出现的日期,并且只保留“关闭”列?就像是

                 AAE      AAR
date
1994-02-21  20.47626  0.34000
1994-02-22  19.68871  0.34100
4

2 回答 2

1

似乎与waitkuo得到了相同的结果,但分为几个步骤:

In[127]:
    aae = data['AAT'].close 
    aar = data['AAR'].close 
    df = pd.DataFrame({'AAE': aae, 'AAR': aar}) 
    df = df.dropna() 

    print aae.head() 
    print aar.head() 
    df.head()

Out[127]:
    date
    1992-01-20    1.56073
    1992-01-22    1.56073
    1992-01-24    1.56073
    1992-01-31    1.56073
    1992-02-03    1.56073
    Name: close
    date
    1992-01-02    0.11657
    1992-01-03    0.11657
    1992-01-07    0.12628
    1992-01-09    0.12628
    1992-01-10    0.13600
    Name: close

                    AAE      AAR
    1992-01-24  1.56073  0.12628
    1992-01-31  1.56073  0.12628
    1992-02-03  1.56073  0.12628
    1992-02-04  1.56073  0.13600
    1992-02-12  1.56073  0.13600
于 2013-05-08T12:06:36.183 回答
1

尝试使用合并来实现您的目标。例如:

pd.merge(aae.reset_index()[['date', 'close']], aar.reset_index()[['date', 'close']], on=['date'])


 Out[129]:
                   date  close.x  close.y
 0  1992-01-24 00:00:00  1.56073  0.12628
 1  1992-01-31 00:00:00  1.56073  0.12628
 2  1992-02-03 00:00:00  1.56073  0.12628
 3  1992-02-04 00:00:00  1.56073  0.13600
 4  1992-02-12 00:00:00  1.56073  0.13600
于 2013-05-08T06:43:23.227 回答