2016-06-30 29 views
3

什麼是合併的面板數據,如下面的電流,最好的大熊貓食譜:熊貓面板合併

p = pd.Panel(np.random.randn(2,5,4), 
    items=['IBM', 'AA'], 
    major_axis=pd.date_range('1/1/2000', periods=5), 
    minor_axis=['Open', 'High', 'Low', 'Close']) 
dp = pd.Panel(np.random.randn(2,1,1), 
    items=['IBM', 'Z'], 
    major_axis=pd.date_range('1/8/2000', periods=1), 
    minor_axis=['Close']) 

預期的合併是這樣的:

p[:,:,'Close'].merge(dp[:,:,'Close'], 
    how='outer', 
    on=list(set(p.items) & set(dp.items)), 
    left_index=True, 
    right_index=True) 

但是,我不知道了解如何有效更新原始面板p,以包含此合併。

如果打印(P [:,:, '關閉'])是這樣的:

    IBM  AA 
2000-01-01 0.190049 0.200745 
2000-01-02 -0.239746 -0.434157 
2000-01-03 -0.112571 -0.302251 
2000-01-04 -1.764957 -0.810951 
2000-01-05 -0.961327 1.436247 

接着上面的表格合併將是這個樣子:

    IBM  AA   Z 
2000-01-01 0.190049 0.200745  NaN 
2000-01-02 -0.239746 -0.434157  NaN 
2000-01-03 -0.112571 -0.302251  NaN 
2000-01-04 -1.764957 -0.810951  NaN 
2000-01-05 -0.961327 1.436247  NaN 
2000-01-08 0.006128  NaN 0.383452 

謝謝。

回答

1

我會轉換成數據幀,combine_first,並再次

new = p.to_frame().combine_first(dp.to_frame()).to_panel() 

print new[:,:,'Close'] 

        AA  IBM  Z 
major         
2000-01-01 1.348884 0.472272  NaN 
2000-01-02 1.599357 -0.228739  NaN 
2000-01-03 2.041504 -0.325773  NaN 
2000-01-04 0.348960 -0.451274  NaN 
2000-01-05 -1.902347 0.146647  NaN 
2000-01-08  NaN -0.240884 0.39855 
+0

好極了!謝謝!完善! –