Python 根据id'比较两个数据帧列;数据帧中的数据类型
我对python完全陌生。我有两个数据帧,它们属于同一个数据集,但一个是输入,一个是输出 这是我的输入数据框Python 根据id'比较两个数据帧列;数据帧中的数据类型,python,python-3.x,pandas,numpy,Python,Python 3.x,Pandas,Numpy,我对python完全陌生。我有两个数据帧,它们属于同一个数据集,但一个是输入,一个是输出 这是我的输入数据框 Document_ID OFFSET PredictedFeature 0 0 2000 0 8 2000 0 16 2200 0 23 2200 0 30 220
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模型的输入。它只给我一个这种格式的输出
现在我的输出看起来像
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.
谁能帮我一下吗?任何事情都会有帮助的 如果两个数据帧
s之间的索引值相同,并且前两列中的值也相同,则使用:
inputdf['new'] = inputdf['PredictedFeature'] == outputdf['PredictedFeature']
海螺
分组
>>> 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试图在数据帧中的切片副本上设置值。尝试改用.loc[row\u indexer,col\u indexer]=值error@ganeshkaspate-你能检查一下吗?有没有办法让我知道,如果我在输入csv中有50条2100条记录,那么如果25条记录匹配了,我就会从中退出?@ganeshkaspate-hmm,所以解决方案失败了?或者你要求别的什么?你能告诉我有什么办法吗?是的,它说的是真的。对不起,当我做比较时,我在这里做了一点错误,因为你给出了早期的解决方案,它说的是假的
>>> df.reindex(idx)
Date Fruit Num Color
9 2013-11-25 Orange 8.6 Orange
8 2013-11-25 Apple 22.1 Red