1

我正在使用 excel 来比较三列:我的想法是将两列数据与第三列作为数组进行比较,就像第三列中的每个值都应该与第一列和第二列的每一行进行比较,并且只想提取第三列中存在第一列和第二列数据的那些行我使用了这个python命令

if([x in x,y for datafile] == [x in x for file) and [y in x,y for datafile] == [x in x for file]): 
    print x,y
else:
    print none        

这给了我一个语法错误

zip我已经使用x,y 对应于元组中的值的函数将前两列转换为元组

Col_1 ||  Col_2    ||   file
Abc   |    Abk     |    cnl
Nck   |    Nck     |    Abk
xkl   |    cnl     |    Abc  
mzn   |    mzn     |  

这个我已经合并为数据文件((Abc,Abk),(Nck,Nck),(xkl,cnl),(mzn,mzn))

注意:我的第 3 列的值小于第 1 列和第 2 列。我有超过 100k 的值要比较

我想要这个查询的工作 python 程序

if [x for x,y in mydata if x == genelist and
y for x,y in mydata if y == genelist]:
    print (x,y)
else: 

有人可以在此处更正上述代码中的语法错误吗

mydata('gene1,genea','gene2,geneb''gene3,genec') and genelist ('genea','geneb','genec') 

当我使用没有 if 语句的代码时,它会打印我“[]”我不知道这里有什么问题

4

1 回答 1

1

您可以使用pandas.Series.isin过滤它:

对于您的 excel 数据 ( eg:comparison.xlsx) :

在此处输入图像描述

采用:

import pandas as pd
df = pd.read_excel('comparison.xlsx')
result = df[df['finaldata1'].isin(list(df['check'])) & df['finaldata2'].isin(list(df['check']))]
result

它会给你:

    finaldata1  finaldata2  check
0   Abc         Abk         cnl

AbcAbk列中一样file

更新:将结果写入 excel 文件:

from pandas import ExcelWriter

writer = ExcelWriter('PythonExport.xlsx')
result.to_excel(writer,'Sheet1',index=False)
writer.save()

结果将写入excel文件PythonExport.xlsx

在此处输入图像描述

于 2017-05-15T01:53:26.427 回答