Skip to main content
 首页 » 编程设计

python之根据数据框中的 id 比较两个数据框列

2024年11月01日7myhome

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

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

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

有人可以帮我解决这个问题吗?任何事情都会有所帮助。

请您参考如下方法:

如果 DataFrame 之间有相同的索引值,并且前两列中的值也相同,请使用:

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