gpt4 book ai didi

python - 如何在日期时间索引上加入两个数据帧,使用 nan 自动填充不匹配的行

转载 作者:太空狗 更新时间:2023-10-30 02:04:15 24 4
gpt4 key购买 nike

x_index=pd.date_range(dt.date(2010,1,1),dt.date(2010,1,5))
y_index=pd.date_range(dt.date(2010,1,2),dt.date(2010,1,6))
x = pd.DataFrame({"AAPL":[1,2,3,4,5]}, index=x_index)
y = pd.DataFrame({"GE": [1,2,3,4,5]}, index=y_index)

结果应该是:

            AAPL GE
2010-01-01 1 nan
2010-01-02 2 1
2010-01-03 3 2
2010-01-04 4 3
2010-01-05 5 4
2010-01-06 nan 5

最佳答案

由于您没有公共(public)列,因此您需要指定使用两个数据帧的索引,并且您希望执行“外部”合并:

In [226]:

x.merge(y, how='outer', left_index=True, right_index=True)
Out[226]:
AAPL GE
2010-01-01 1 NaN
2010-01-02 2 1
2010-01-03 3 2
2010-01-04 4 3
2010-01-05 5 4
2010-01-06 NaN 5

[6 rows x 2 columns]

关于python - 如何在日期时间索引上加入两个数据帧,使用 nan 自动填充不匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22716177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com