[英]How to only add rows from one dataframe to another where values don't match in certain columns
I have two dataframe, df1 and df2 (shown below) and I wanted df3.我有两个 dataframe,df1 和 df2(如下所示),我想要 df3。 So basically, if duplicates appear between both and column "Complete" == 'C' then remove row from df1, otherwise keep df1 rows and add remaining rows from df2.
所以基本上,如果重复出现在“完成”列 == 'C' 之间,则从 df1 中删除行,否则保留 df1 行并从 df2 添加剩余行。 Hopefully this makes sense?
希望这是有道理的? There may be a simple way to do this and I'm just making sound more complicated than it actually is!?
可能有一种简单的方法可以做到这一点,而我只是让声音比实际更复杂!?
df1:
Complete Name Birth
C Steve 13/07/2000
C Mike 13/06/2000
C Sarah 20/05/1936
C Lewis 14/08/1955
NaN Martin 15/04/1990
NaN Lewis 15/04/1990
df2:
Complete Name Birth
NaN Steve 13/07/2000
NaN Mike 13/06/2000
NaN Sarah 20/05/1936
NaN Lewis 14/08/1955
NaN Martin 15/04/1990
NaN Lewis 15/04/1990
NaN Dave 13/04/1935
NaN Mark 14/07/1932
NaN Steve 15/06/1970
I wish for df1 to therefore become:我希望 df1 因此成为:
Complete Name Birth
NaN Martin 15/04/1990
NaN Lewis 15/04/1990
NaN Dave 13/04/1935
NaN Mark 14/07/1932
NaN Steve 15/06/1970
# merge both dataframes, 2 tricks, .reset_index()...set_index() will keep the original index and not reset him
# trick 2, use indicator=True which creates the column "_merge" where you can see in which dataframe the rows where found, left, right or both
df = df1.reset_index().merge(df2, on=["Complete", "Name", "Birth"], how="left", indicator=True).set_index("index")
# creates a mask (series with True / False values)
mask = (df["_merge"]=="both") & (df["Complete"] == "C")
# only keep rows where mask == True, the "~" inverts the boolean value, therefore excludes the mask
df = df[~mask]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.