簡體   English   中英

根據 dataframe 中的 id 比較兩個數據幀列

[英]Compare the two data-frames columns on the basis of id's in the dataframe

我對 python 完全陌生。 我有兩個數據幀,它們屬於相同的數據集,但一個是輸入的,一個是 output。

所以,這是我的輸入 dataframe

Document_ID OFFSET  PredictedFeature
    0         0            2000
    0         8            2000
    0         16           2200
    0         23           2200
    0         30           2200
    1          0            2100
    1          5            2100
    1          7            2100

所以在這里,我將其作為我的ml-model的輸入。 它僅以這種格式給了我一個 output。

現在我的 output 看起來像,

  Document_ID    OFFSET   PredictedFeature
        0         0            2000
        0         8            2000
        0         16           2100
        0         23           2100
        0         30           2200
        1          0           2000
        1          5           2000
        1          7           2100

現在,在這兩個數據框中,我想做的是

對於該 ID,對於該偏移量,輸入功能與 output 功能的輸入功能相同。 如果是,那么我想在新列中添加 true 作為值,如果不是,那么它將添加 false 值。

現在,如果我們在示例數據中看到

for ID 0 , for offset 16 the input feature is 2200 and output feature is 2100 so it is a false.

有人可以幫我嗎? 任何事情都會有所幫助。

如果DataFrame和前 2 列中的值相同,則使用相同的索引值:

inputdf['new'] = inputdf['PredictedFeature'] == outputdf['PredictedFeature']

連接

>>> df = pd.concat([df1, df2])
>>> df = df.reset_index(drop=True)

通過...分組

 >>> df_gpby = df.groupby(list(df.columns))

獲取唯一記錄的索引

>>> idx = [x[0] for x in df_gpby.groups.values() if len(x) == 1]

篩選

>>> df.reindex(idx)
         Date   Fruit   Num   Color
9  2013-11-25  Orange   8.6  Orange
8  2013-11-25   Apple  22.1     Red

使用這個方法你可以通過索引值找出不同的數據,你可以為這個索引值添加新列,只有 false 另一個值是 true

暫無
暫無

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

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