python - pd.concat() 未在同一索引上合并
问题描述
我有一个包含预测的 DataFrame 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])
但我收到一个未在索引上对齐的 DataFrame:
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()
返回真。
我的问题是:为什么两个 DataFrame 没有在索引上连接,我该怎么做才能解决这个问题?
我知道 join 函数,但是由于我计划添加的其他一些 DataFrame 中缺少某些日期,我相信该concat
函数可能会更好。
解决方案
调用concat
设置axis
为1
:
pd.concat([df1, df2], axis=1)
推荐阅读
- android - React Native:本机方法 ExpoLocalAuthentication.authenticateAsync 需要 0 个参数但收到 1
- html - 使用 CSS (Vue) 将 Html 元素完全插入到 div 中
- apache-kafka - Zookeeper:cnxn.saslServer 为空,Kafka:仲裁成员的 saslToken 为空
- python - Gensim 矢量形状变化
- swift - 对手视频未在 Twilio 视频中显示
- json - 在 jq 命令的输出中附加字符串和特殊字符
- python - 如何在openpyxl中使两列相互连接
- java - 端口无效。Exiting..Exception in thread "main" org.openqa.selenium.WebDriverException:驱动程序服务器进程过早死亡 ChromeDriver Selenium
- find - 使用 print0 时输出随 find 变化
- javascript - React Router Route 仅从配置中渲染第一条路由