使用python cod的Excel列比较

2024-06-23 19:57:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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列。我有超过10万个值要比较

我想要一个可用于此查询的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语句的情况下使用代码时,它会打印出“[]”我不知道这里出了什么问题


Tags: and数据inforiffileabcprint
1条回答
网友
1楼 · 发布于 2024-06-23 19:57:25

您可以使用pandas.Series.isin对其进行筛选:

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

enter image description here

使用:

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

因为AbcAbkfile列中。

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

from pandas import ExcelWriter

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

结果将写入excel文件PythonExport.xlsx

enter image description here

相关问题 更多 >

    热门问题