python - 连接两个数据框,具有一些重叠的日期索引,结果数据框默认采用“左”,除了“左”是 NaN
问题描述
下面给出 DF1 和 DF2,如何得到 DFresult?
DF1: DF2: DFresult:
Date | Value Date | Value Date | Value
------------ ------------ ------------
1-01-2019 | 1 1-01-2019 | 1 (no overlap, take the one that exists)
1-02-2019 | 1 1-02-2019 | 1 (no overlap, take the one that exists)
1-03-2019 | np.NaN 1-03-2019 | 2 1-03-2019 | 2 (left is NaN, take right)
1-04-2019 | 1 1-04-2019 | np.NaN 1-04-2019 | 1 (left is not NaN, take left)
1-05-2019 | np.NaN 1-05-2019 | np.NaN 1-05-2019 | np.NaN (both NaN, keep it)
1-06-2019 | 1 1-06-2019 | 2 1-06-2019 | 1 (left is not NaN, take left)
1-07-2019 | 2 1-07-2019 | 2 (no overlap, take the one that exists)
1-08-2019 | 2 1-08-2019 | 2 (no overlap, take the one that exists)
1-09-2019 | 2 1-09-2019 | 2 (no overlap, take the one that exists)
1-10-2019 | 2 1-10-2019 | 2 (no overlap, take the one that exists)
1-11-2019 | 2 1-11-2019 | 2 (no overlap, take the one that exists)
如果我想使用函数来确定重叠决策怎么办?例如,如果 left 高于 right,或者 left 是 NaN,则向左:
DF1: DF2: DFresult:
Date | Value Date | Value Date | Value
------------ ------------ ------------
1-01-2019 | 1 1-01-2019 | 1 (no overlap, take the one that exists)
1-02-2019 | 1 1-02-2019 | 1 (no overlap, take the one that exists)
1-03-2019 | np.NaN 1-03-2019 | 2 1-03-2019 | 2 (left is NaN, take right)
1-04-2019 | 1 1-04-2019 | np.NaN 1-04-2019 | 1 (right is NaN, take left)
1-05-2019 | np.NaN 1-05-2019 | np.NaN 1-05-2019 | np.NaN (both NaN, keep it)
1-06-2019 | 1 1-06-2019 | 2 1-06-2019 | 2 (left is not higher, take right)
1-06-2019 | 3 1-07-2019 | 2 1-07-2019 | 3 (left is higher, take left)
1-06-2019 | 1 1-08-2019 | 2 1-08-2019 | 2 (left is not higher, take right)
1-09-2019 | 2 1-09-2019 | 2 (no overlap, take the one that exists)
1-10-2019 | 2 1-10-2019 | 2 (no overlap, take the one that exists)
1-11-2019 | 2 1-11-2019 | 2 (no overlap, take the one that exists)
解决方案
尝试
out = pd.concat([DF1,DF2]).groupby('Date',as_index=False).max()
# for your original one
#out = pd.concat([DF1,DF2]).groupby('Date',as_index=False).first()
推荐阅读
- java - 如何从 Stream 更改
流式传输 爪哇 8 - php - 如何将响应发送回 AJAX 并继续 php?
- api - 使用 Jenkins Docker 插件动态创建的 Jenkins Slave 在作业执行过程中被删除
- c# - .net core 3.0 app.UseExceptionHandler 没有在错误方法中达到断点
- c# - 使用 Castle.Windsor 和 Polly 响应 429 异常(节流)
- python - 使用变量值的 SQL
- javascript - 为什么我的 for 循环不在我的 getJSON 请求中执行?
- google-chrome - Blackmagic 设备和 WebRTC 不兼容
- scala - 在 Gatling/Scala 中,如何将 jsonPath 转换为数组?
- c++ - 为什么 std::swap 不适用于向量
Clang/Win 下的元素?