简体   繁体   中英

Create a dataframe by discarding intersections of two dataframes (Pandas)

Does anyone know of an efficient way to create a new dataframe based off of two dataframes in Python/Pandas?

What I am trying to do is check if a value from df1 is in df2, then do not add the row to df3. I am working with student IDS, and if a student ID from df1 is in df2, I do not want to include it in the new dataframe, df3.

So does anybody know an efficient way to do this? I have googled and looked on SO, but found nothing that works so far.

假设该列称为ID。

df3 = df1[~df1["ID"].isin(df2["ID"])].copy()

If you have both dataframes of same length you can also use:

print df1.loc[df1['ID'] != df2['ID']]

assign it to a third dataframe.

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