当我本来应该阅读如何推送和推送数据帧数据时,我真诚地后悔去了酒吧:
鉴于两个相似的形状数据框,我该如何组合它们?
> 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
答案 0 :(得分:1)
尝试使用merge来实现目标。例如:
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
答案 1 :(得分: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