欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python concat_python-pd.concat()不合并在同一索引上

发布时间:2024/7/5 python 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python concat_python-pd.concat()不合并在同一索引上 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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函数可能会更好.

谢谢!

总结

以上是生活随笔为你收集整理的python concat_python-pd.concat()不合并在同一索引上的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。