簡體   English   中英

檢查 dataframe 中的行是否存在於另一個 dataframe 中並從兩者中刪除

[英]Check if row in dataframe exists in another dataframe and remove from both

我正在嘗試檢查一個 dataframe 中的行是否存在於另一個中,如果是,我想從兩個數據幀中刪除它們。 到目前為止,我看到的所有示例都使用 pd.merge,但它合並為一個 dataframe。 我的目標是保留兩個單獨的數據框並刪除公共行。

示例如下:

df1:
                id            name             class        Grade
0             2547            John             Math         119.01
1             2547            Joe              Science      0.00
2             2547            Steve            History      0.47
3             2547            Hari             PE           5.70

df2:
                id            name             class        Grade
0             2547            John             Math         119.01
1             2547            Joe              Science      2
2             2547            Steve            History      22
3             2547            Hari             PE           5.71

expected output:

    df1:
                    id            name             class        Grade
    0             2547            Joe              Science      0.00
    1             2547            Steve            History      0.47
    2             2547            Hari             PE           5.70
    
    df2:
                    id            name             class        Grade
    0             2547            Joe              Science      2
    1             2547            Steve            History      22
    2             2547            Hari             PE           5.71

到目前為止,我嘗試如下,但這沒有幫助,因為它合並了兩個數據框:

df = pd.merge(df1, df2, on=['Grade'], how='outer')

您可以使用inner合並 append 將公共行存儲到兩個數據幀,並刪除重復項而不保留任何重復項drop_duplicates(keep=False)

t = df1.merge(df2,'inner')
df2, df1 = df2.append(t).drop_duplicates(keep=False) , df1.append(t).drop_duplicates(keep=False)

印刷:

>>> df1
 
     id   name    class  Grade
1  2547    Joe  Science   0.00
2  2547  Steve  History   0.47
3  2547   Hari       PE   5.70

>>> df2

     id   name    class  Grade
1  2547    Joe  Science   2.00
2  2547  Steve  History  22.00
3  2547   Hari       PE   5.71

>>> t
 
     id  name class   Grade
0  2547  John  Math  119.01

使用from_frame方法創建MultiIndex對象,然后使用MultiIndex.isin檢查成員資格以創建 boolean 掩碼以過濾行

i1 = pd.MultiIndex.from_frame(df1)
i2 = pd.MultiIndex.from_frame(df2)

df1, df2 = df1[~i1.isin(i2)], df2[~i2.isin(i1)]

>>> df1
     id   name    class  Grade
1  2547    Joe  Science   0.00
2  2547  Steve  History   0.47
3  2547   Hari       PE   5.70

>>> df2
     id   name    class  Grade
1  2547    Joe  Science   2.00
2  2547  Steve  History  22.00
3  2547   Hari       PE   5.71
   Try this -



 dataframe1 = pd.DataFrame(data={"column1": [1, 2, 3, 4, 5]})
    
    dataframe2 = pd.DataFrame(data={"column1": [1, 2]})
    
    
    common = dataframe1.merge(dataframe2, on=["column1"])
    
    result = dataframe1[~dataframe1.column1.isin(common.column1)]
    
    
    print(result)

有關更多詳細信息,請參閱本文 - https://www.kite.com/python/answers/how-to-get-rows-from-a-dataframe-that-are-not-in-another-dataframe-in-python

或者你可以你這個只是為了檢查-

df = df1.merge(df2, on='grade', suffixes=(' from df1',' from df2'))
df.insert(0, 'id', df['grade'] + '-' + df.pop('grade'))
print (df)

這是使用isinall的簡單方法:

df3 = df1[~df1.isin(df2).all(axis=1)]
df4 = df2[~df2.isin(df1).all(axis=1)]
df1 = df3
df2 = df4

要不就,

df1, df2 = df1[~df1.isin(df2).all(axis=1)], df2[~df2.isin(df1).all(axis=1)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM