我有两个数据帧df1和df2 我想覆盖两个DataFrame中相同的行。 我想在合并后用df2覆盖df1的匹配行 这是我的数据
val resDF2 = aggDF.select(cols.map { col => when(size(aggDF(col)) > 0,aggDF(col)).as(s"$col") }: _*)
如df3中所示。在附加/合并df1和df2之后,我想用df2值将df1与df2匹配的行覆盖。因此,我不应获取重复的行
我想覆盖,因为列值可能会有所不同。 请帮助。我正在寻找解决方案,但库伦为此找到了更好的解决方案。 df3中的覆盖行位于第二行(第二行)
答案 0 :(得分:1)
我们可以使用pd.concat
+ DataFrame.drop_duplicates
或groupby.first
:
pd.concat([df2,df1]).drop_duplicates(subset = ['Date','Indicator'],keep='first').reset_index(drop=True)
#pd.concat([df2,df1]).groupby(['Date','Indicator'],as_index=False).first()
Date Value Indicator Frequency Measure Unit
0 2002-08-21 21.236000 KN.A11 A KN.M4 index,2000=100
1 2002-08-28 91.946309 KN.A10 W KN.M3 index,2000=100
2 2002-09-04 24.589000 KN.A11 A KN.M4 index,2000=100
3 2002-09-11 62.254000 KN.A11 A KN.M4 index,2000=100
4 2002-09-18 15.254000 KN.A11 A KN.M4 index,2000=100
0 2002-08-21 89.463087 KN.A10 W KN.M3 index,2000=100
2 2002-09-04 93.154362 KN.A10 W KN.M3 index,2000=100
3 2002-09-11 93.691275 KN.A10 W KN.M3 index,2000=100
4 2002-09-18 94.899329 KN.A10 W KN.M3 index,2000=100