python - Pandas 面板合并

标签 python pandas merge

合并面板数据的当前最佳 Pandas 方法是什么,例如:

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 以包含此合并。

如果 print(p[:,:,'Close']) 是这样的:

                 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

谢谢。

最佳答案

我会转换为数据帧,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

关于python - Pandas 面板合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129958/

相关文章:

git - Git merge 如何处理同时提交?

python - Python 中两个列表的异或

python - 我无法向电报机器人用户发送消息,但我自己

python - 访问数据框的最后一个索引值

python - 用于列表理解 Python 的多个条件语句

delphi - 在其他 PNG 上绘制 PNG 图像

mysql - 数据库合并表

python - SQLAlchemy:排除从查询的子查询中获取的行

python - 合并列并删除重复项

python - Pandas:从 Pandas DataFrame 中选择两个日期之间的所有数据