[英]How to merge these two data frames?
I have two data frames DF1, DF2 that I want to merge on certain conditions. 我有两个要在特定条件下合并的数据帧DF1,DF2。 They are both indexed by dates, but of different lengths.
它们都由日期索引,但长度不同。 I want to get a data frame similar to DF1 but where for a given date the value is modified to the value of DF2 if and only if there is a value for the same date in DF2.
我想获得一个类似于DF1的数据框,但是当且仅当DF2中存在相同日期的值时,给定日期的值才会修改为DF2的值。
From 从
DF1
v
2014-01-02 0.580550
2014-01-03 -1.961033
2014-01-04 2.063441
2014-01-05 0.319570
2014-01-06 1.318068
And 和
DF2
v
2014-01-02 10.0
2014-01-03 14.0
2014-01-06 -5.3
Get 得到
DF
v
2014-01-02 10.0
2014-01-03 14.0
2014-01-04 2.063441
2014-01-05 0.319570
2014-01-06 -5.3
You can use combine_first
: 您可以使用
combine_first
:
DF = DF2.combine_first(DF1)
print (DF)
v
2014-01-02 10.000000
2014-01-03 14.000000
2014-01-04 2.063441
2014-01-05 0.319570
2014-01-06 -5.300000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.