首页 > 解决方案 > 根据数据框中的 id 比较两个数据框列

问题描述

我对 python 完全陌生。我有两个数据框属于相同的数据集,但一个是输入,一个是输出。

所以,这是我的输入数据框

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. 它只给我这种格式的输出。

现在我的输出看起来像,

  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,对于该偏移量,输入特征与输出特征相同。如果是,那么我想在新列中添加 true 作为值,如果不是,那么它将添加 false 值。

现在,如果我们在示例数据中看到

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

有人可以帮我吗?任何事情都会有所帮助。

标签: pythonpython-3.xpandasnumpy

解决方案


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

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

推荐阅读