【发布时间】:2023-04-02 22:54:01
【问题描述】:
我是 Python 新手,我准备了一个脚本,将修改以下 csv file
因此:
1) 每行包含多个 Gene 条目,以/// 分隔,如:
C16orf52 /// LOC102725138 1.00551
应该转化为:
C16orf52 1.00551
LOC102725138 1.00551
2) 同一个基因可能有不同的比值
AASDHPPT 0.860705
AASDHPPT 0.983691
我们只想保留比率值最高的对(删除对AASDHPPT 0.860705)
这是我编写的脚本,但它没有为基因分配正确的比率值:
import csv
import pandas as pd
with open('2column.csv','rb') as f:
reader = csv.reader(f)
a = list(reader)
gene = []
ratio = []
for t in range(len(a)):
if '///' in a[t][0]:
s = a[t][0].split('///')
gene.append(s[0])
gene.append(s[1])
ratio.append(a[t][1])
ratio.append(a[t][1])
else:
gene.append(a[t][0])
ratio.append(a[t][1])
gene[t] = gene[t].strip()
newgene = []
newratio = []
for i in range(len(gene)):
g = gene[i]
r = ratio[i]
if g not in newgene:
newgene.append(g)
for j in range(i+1,len(gene)):
if g==gene[j]:
if ratio[j]>r:
r = ratio[j]
newratio.append(r)
for i in range(len(newgene)):
print newgene[i] + '\t' + newratio[i]
if len(newgene) > len(set(newgene)):
print 'missionfailed'
非常感谢您的任何帮助或建议。
【问题讨论】:
-
嗨 Manolis,也许你应该了解一下How to create a Minimal, Complete, and Verifiable example
-
我认为理想情况下您可能希望将基因存储在字典中,并且在分配值时,如果键退出,如果它不大于当前值,则忽略。
标签: python arrays csv optimization