我有一个 df 包含名为 fcst 的预测,如下所示:

             yhat        yhat_lower  yhat_upper
ds
2015-08-31  -0.443522   -19.067399  17.801234
2015-09-30  6.794625    -31.472186  46.667981
...

进行此转换后:
fcst2 = fcst["yhat"].to_frame().rename(columns={"yhat":"test1"})
fcst3 = fcst["yhat"].to_frame().rename(columns={"yhat":"test2"})

我想在日期索引上将它们连接起来:
pd.concat([fcst2,fcst3])

但是我收到一个未在索引上对齐的数据框:
             test1     test2
ds
2015-08-31  -0.443522   NaN
2015-09-30  6.794625    NaN
... ... ...
2017-05-31  NaN 95.563262
2017-06-30  NaN 85.829916

这尽管:
(fcst2.index == fcst3.index).any()

返回 True。

我的问题是:为什么两个数据帧没有连接到索引上,我该怎么做才能解决这个问题?

我知道 join 函数,但是由于我计划添加的其他一些数据帧中会缺少某些日期,因此我相信 concat 函数可能会更好。

谢谢!

最佳答案

调用 concat 并将 axis 设置为 1 :

pd.concat([df1, df2], axis=1)

关于python - pd.concat() 未在同一索引上合并,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52948106/

10-12 16:51