我有两个大小不同的数据框:
ConceptID1 ConceptID2
0 5743 4513
1 5743 7099
2 4513 7099
3 10242 7042
4 10242 7099
... ... ...
2601 12028 12043
2602 12371 12043
2603 266632 54106
2604 266632 51135
2605 54106 51135
Gene1 Gene2
0 1535 353
1 9970 332
2 23581 112401
3 846 112401
4 150160 112401
.. ... ...
384 79626 51284
385 79626 51311
386 7305 51311
387 80342 79626
388 7305 79626
通过两个数据帧进行比较,我需要找到匹配对。
我试过了
for index, row in sdfn.iterrows():
for index, row in jdfn.iterrows():
if ((sdfn['ConceptID1']==jdfn['Gene1']) and (sdfn['ConceptID2']==jdfn['Gene2'])) or (sdfn['ConceptID1']==jdfn['Gene2']) and ((sdfn['ConceptID2']==jdfn['Gene1'])):
print(sdfn['ConceptID1'], jdfn['Gene1'], sdfn['ConceptID2'], jdfn['Gene2'])
结果:
追溯(最近一次通话):
文件“”,第3行,在
if ((sdfn['ConceptID1']==jdfn['Gene1']) and (sdfn['ConceptID2']==jdfn['Gene2'])) or
(sdfn ['ConceptID1'] == jdfn ['Gene2'])和(((sdfn ['ConceptID2'] == jdfn ['Gene1'])):文件“ /Library/Frameworks/Python.framework/Versions/3.7 /lib/python3.7/site-packages/pandas/core/ops/ init .py“,行1142,在包装器中引发ValueError(”只能比较标记相同的“系列对象”)
ValueError:只能比较标记相同的Series对象
这里的问题是您没有for
正确使用或命名循环变量,而是试图直接比较每个数据框列的整体。
sdfn['ConceptID1']
,sdfn['ConceptID2']
,jdfn['Gene1']
,jdfn['Gene2']
将引用整个dataframe列,pandas将其定义为Series
类型对象,因此Series
在错误消息中提到了标签不匹配。
您将需要先重命名for
循环变量,然后在搜索中使用它们:
for sind, srow in sdfn.iterrows():
for jind, jrow in jdfn.iterrows():
if ((srow['ConceptID1']==jrow['Gene1']) and (srow['ConceptID2']==jrow['Gene2'])) or (srow['ConceptID1']==jrow['Gene2']) and ((srow['ConceptID2']==jrow['Gene1'])):
print(srow['ConceptID1'], jrow['Gene1'], srow['ConceptID2'], jrow['Gene2'])
请注意,在您发布的代码,index
以及row
变量的声明,并在外环分配尚未在内部循环被修改。因此,只有两对循环变量而不是具有两对循环变量,因此无法比较适当的数据。
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句