简体   繁体   中英

Python delete row from a DataFrame based on another DataFrame with less variables

I have df1 like this:

id 1  2  3  4  5
0  1  1  0  0  0 
1  1  0  1  0  0
2  1  0  0  0  1

The I have df (less columns, less cases) with this values:

id 1  2  5  
0  1  1  0
1  1  0  1

I would like to delete from df1 the rows that share the same values as the ones from df2, so final df looks like this:

id 1  2  3  4  5
1  1  0  1  0  0

I'm deleting 2 rows since df1 and df2 shared the same values on their corresponding columns.

Thank you!

This will solve your problem:

print (pd.merge(df1,df2, indicator=True, how='outer')
         .query('_merge=="left_only"')
         .drop('_merge', axis=1))

I hope this can help you in finding a solution . df2 is a dataframe with the intersection of the other two based on the three same columns. cleared_df is the initial df except the intersection.

#Replicating the question's input
d={1:[1,1,1],2:[1,0,0],3:[0,1,0],4:[0,0,0],5:[0,0,1]}
d1={1:[1,1],2:[1,0],5:[0,1]}
df = pd.DataFrame(data=d)
df1 = pd.DataFrame(data=d1)
#Make df with the same records on 1,2,5
df2=pd.merge(df, df1, on=[1,2,5], how='inner')
#Concat the initial df with the one with the same records, then drop the duplicates
cleared_df=pd.concat([df, df2]).drop_duplicates(keep=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM