熊猫合并而不会覆盖

时间:2019-10-14 15:50:43

标签: pandas dataframe merge

我有两个数据框。我正在尝试将数据从DF1合并到DF2,而不更改DF2中以前存在的任何数据。

DF1 = 
Value    Color
0         Red
1         White
2         Blue

DF2 = 
Value    Color
0         
1         
2         Brown
3         Black
4         White
5         

DF3 = pd.merge(DF2, DF1, on="Value", how='left', suffixes=('_x', '')).drop(['Color_x'], axis=1)

当前结果:它将覆盖已经存在的数据。例如,它正确地将棕色覆盖为蓝色。但是,由于DF1中不存在这些值,因此它也会删除“黑白”。我希望它只是合并或覆盖我们要在匹配项上合并的值。

DF3 = 
Value    Color
0         Red
1         White
2         Blue
3         
4         
5         

预期结果:

DF3 = 
Value    Color
0         Red
1         White
2         Blue
3         Black
4         White
5         

2 个答案:

答案 0 :(得分:5)

您寻找 <match input> @type copy <store> @type rewrite_tag_filter <rule> key type pattern /^\[(\w+)\]/ tag ${tag}.$1 </rule> </store> <store> @type rewrite_tag_filter <rule> key type pattern /^\[(\w+)\]/ tag ${tag}.$1 </rule> </store> </match> <match input.click> @type copy <store> @type file path /tmp/fluent/click append true ## compress gzip </store> <store> @type mongo host localhost port 27017 database fluentd collection click <buffer> # flush flush_interval 10s </buffer> </store> </match> <match input.show> @type copy <store> @type file path /tmp/fluent/show append true ## compress gzip </store> <store> @type mongo host localhost port 27017 database fluentd collection show <buffer> # flush flush_interval 10s </buffer> </store> </match>

update

注意:此答案基于您的示例数据,其中df2.update(df1) print(df2) Out[253]: Value Color 0 0.0 Red 1 1.0 White 2 2.0 Blue 3 3.0 Black 4 4.0 White 5 5.0 索引或df1value的子集。您没有提到索引,因此我假设它是默认的df2。如果索引不是rangeindex,则需要在rangeindex上使用set_index

value

答案 1 :(得分:2)

我们可以使用combine_first

df1.set_index('Value').combine_first(df2.set_index('Value')).reset_index()
   Value  Color
0      0    Red
1      1  White
2      2   Blue
3      3  Black
4      4  White
5      5    NaN