保留具有特定列的最大值的行

时间:2017-07-21 17:29:55

标签: python arrays csv sorting

我是Python新手,我想做以下事情。我有一个csv文件(input.csv),其中包含标题行和4列。这个csv文件的一部分如下所示:

gene-name p-value stepup(p-value) fold-change
IFIT1 6.79175E-005 0.0874312 96.0464
IFITM1 0.00304362 0.290752 86.3192
IFIT1 0.000439152 0.145488 81.499
IFIT3 5.87135E-005 0.0838258 77.1737
RSAD2 6.7615E-006 0.0685623 141.898
RSAD2 3.98875E-005 0.0760279 136.772
IFITM1  0.00176673 0.230063 72.0445

我想只保留折叠更改值最高的行,并删除包含相同基因名称且折叠更改值较低的所有其他行。例如,在这种情况下,我需要一个以下格式的csv输出文件:

gene-name p-value stepup(p-value) fold-change
IFIT1 6.79175E-005 0.0874312 96.0464
IFITM1 0.00304362 0.290752 86.3192
RSAD2 6.7615E-006 0.0685623 141.898   
IFIT3 5.87135E-005 0.0838258 77.1737

如果你能帮我解决这个问题,我将不胜感激 非常感谢你。

2 个答案:

答案 0 :(得分:1)

愚蠢的解决方案:走完文件中的每一行,做一个手动比较。假设:

  • 每列用单个空格分隔
  • 结果行的数量预计会适合内存,因为我们必须在将结果刷新到文件之前完成整个搜索并进行比较
  • 没有预先分类,所以这会缩小(速度),因为它会在每个输入行上完整地查看结果列表。
  • 如果基因以后以某种方式进行相同的倍数更改,您希望保留基因所见的第一行。

::

fi = open('inputfile.csv','r') # read

header = fi.readline() 
# capture the header line ("gene-name p-value stepup(p-value) fold-change")    

out_a = [] # we will store the results in here

for line in fi: # we can read a line this way too
    temp_a = line.strip('\r\n').split(' ') 
    # strip the newlines, split the line into an array

    try:
        pos = [gene[0] for gene in out_a].index(temp_a[0])
        # try to see if the gene is already been seen before
        # [0] is the first column (gene-name)
        # return the position in out_a where the existing gene is
    except ValueError: # python throws this if a value is not found
        out_a.append(temp_a)
        # add it to the list initially
    else: # we found an existing gene
        if float(temp_a[3]) > float(out_a[pos][3]):
            # new line has higher fold-change (column 4)
            out_a[pos] = temp_a
            # so we replace

fi.close() # we're done with our input file
fo = open('outfile.csv','w') # prepare to write to output
fo.write(header) # don't forget about our header
for result in out_a:
    # iterate through out_a and write each line to fo
    fo.write(' '.join(result) + '\n')
    # result is a list [XXXX,...,1234]
    # we ' '.join(result) to turn it back into a line
    # don't forget the '\n' which makes each result on a line

fo.close()

这样做的一个优点是它可以保留输入文件中首次遇到的基因顺序。

答案 1 :(得分:0)

尝试使用pandas:

import pandas as pd

df = pd.read_csv('YOUR_PATH_HERE')

print(df.loc[(df['gene-name'] != df.loc[(df['fold-change'] == df['fold-change'].max())]['gene-name'].tolist()[0])])

代码很长,因为我选择在一行中完成,但代码正在做的是这个。我抓住最高gene-name的{​​{1}},然后使用fold-change运算符说,“抓住!=与{{1}不同的所有内容我们刚才做的计算。

细分:

gene-name

输出:

gene-name

编辑:

获取预期输出使用# gets the max value in fold-change max_value = df['fold-change'].max() # gets the gene name of that max value gene_name_max = df.loc[df['fold-change'] == max_value]['gene-name'] # reassigning so you see the progression of grabbing the name gene_name_max = gene_name_max.values[0] # the final output df.loc[(df['gene-name'] != gene_name_max)]

gene-name   p-value stepup(p-value) fold-change
0   IFIT1   0.000068    0.087431    96.0464
1   IFITM1  0.003044    0.290752    86.3192
2   IFIT1   0.000439    0.145488    81.4990
3   IFIT3   0.000059    0.083826    77.1737
6   IFITM1  0.001767    0.230063    72.0445