Python:获取特定列数据的列标头



我有一些需要处理的以制表符分隔的文本文件。我需要在列"研究"中获得所有唯一值的标题。

例如

:如果我的数据是:

csv1:

name   study   id   race
aaa   cb10   123   asian
bbb   cb10   128 
ccc   vj97   864   

csv2:

name study vaccine
aaa cb10
bbb cb10 abc
ccc vj97 poi
在多个文件中,我的输出应该是'study'列中所有研究的列标题:
cb10- name,study,id,race,vaccine
vj97- name,study,id,vaccine

我有以下代码:

import os
import sys
import glob, ntpath, csv
def get_header_for_tsv_file(tsv_data):
    if not os.path.exists("Results"):
        os.makedirs("Results")
    #output_path = os.path.join ("Results",study + ".csv")
    result = []
    search_for = study
    header = tsv_data.next()
    #output_file = open (output_path, "ab")
    #for row in tsv_data:
    if data["study"] in search_for:
        print data
def path_leaf(path):
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
def get_tsv_list():
    tsv_list = glob.glob(os.getcwd()+"*.txt")
    return tsv_list
def get_tsv_data(tsv_name):
    file_name = os.path.join(tsv_name + ".txt")
    if not os.path.exists(file_name):
        print "Error: Couldn't find file:", file_name
        sys.exit(-1)
    input_data = open (file_name)
    input_data = csv.DictReader(input_data, delimiter = "t")
    return input_data
def run(tsv_name):
    tsv_data = get_tsv_data(tsv_name)
    header_data = get_header_for_tsv_file(tsv_data)
if __name__ == "__main__":
    tsv_list = get_tsv_list()
    filename = [path_leaf(path) for path in tsv_list]
    for index in range(0, len(filename)):
        tsv_name_list = filename[index]
        tsv_name = os.path.splitext(os.path.basename(tsv_name_list))[0]
        tsv_data = get_tsv_data(tsv_name)
        for data in tsv_data:
            study = data["study"]
            run(tsv_name)

我希望使用默认的csv包而不是熊猫,如果可能的话。我能做到吗?

伪代码:

load all file via pandas
take the unique values from the studys - series
make a set from the values above.
Output them

相关内容

  • 没有找到相关文章

最新更新