根据另一列的内容堆叠列

时间:2015-07-09 16:18:29

标签: python-2.7

我有一个包含以下内容的csv文件:

ID  Flow    TestNa  4   5   6
item_1  A   test_1  89  51  67
item_1  A   test_2  60  67  44
item_1  A   test_3  111 82  67
item_1  B   test_1  88  78  75
item_1  B   test_2  104 66  66
item_1  B   test_3  109 87  49
item_2  A   test_1  76  76  88
item_2  A   test_2  59  44  60
item_2  A   test_3  91  72  59
item_2  B   test_1  73  50  80
item_2  B   test_2  107 75  67
item_2  B   test_3  119 100 50

标题为:ID Flow TestNa 4 5 6

我希望按最后三列进行堆叠,这些列是具有以下结果的数据列:

ID    Flow TestNa Label Data
item_1  A   test_1  4   89
item_1  A   test_1  5   51
item_1  A   test_1  6   67
item_1  A   test_2  4   60
item_1  A   test_2  5   67
item_1  A   test_2  6   44
item_1  A   test_3  4   111
item_1  A   test_3  5   82
item_1  A   test_3  6   67
item_1  B   test_1  4   88
item_1  B   test_1  5   78
item_1  B   test_1  6   75
item_1  B   test_2  4   104
item_1  B   test_2  5   66
item_1  B   test_2  6   66
item_1  B   test_3  4   109
item_1  B   test_3  5   87
item_1  B   test_3  6   49
item_2  A   test_1  4   76
item_2  A   test_1  5   76
item_2  A   test_1  6   88
item_2  A   test_2  4   59
item_2  A   test_2  5   44
item_2  A   test_2  6   60
item_2  A   test_3  4   91
item_2  A   test_3  5   72
item_2  A   test_3  6   59
item_2  B   test_1  4   73
item_2  B   test_1  5   50
item_2  B   test_1  6   80
item_2  B   test_2  4   107
item_2  B   test_2  5   75
item_2  B   test_2  6   67
item_2  B   test_3  4   119
item_2  B   test_3  5   100
item_2  B   test_3  6   50

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

稍微强力的选择是:

with open('data-out.csv', 'w') as fo:
    with open('data.csv', 'r') as fi:
        header = fi.readline()
        cols = [s.strip() for s in header.split(',')[-3:]]
        print >>fo, 'ID,Flow,TestNa,Label,Data'
        for line in fi.readlines():
            fields = line.strip().split(',')
            out_fields = fields[:-3]
            for i in range(3):
                print >>fo, ','.join(out_fields + [cols[i], fields[-3+i]])

(当然,如果您缺少数据等,这将是非常宽容的。)

输入data.csv

ID,Flow,TestNa,4,5,6
item_1,A,test_1,89,51,67
item_1,A,test_2,60,67,44
item_1,A,test_3,111,82,67
item_1,B,test_1,88,78,75
item_1,B,test_2,104,66,66
item_1,B,test_3,109,87,49
item_2,A,test_1,76,76,88
item_2,A,test_2,59,44,60
item_2,A,test_3,91,72,59
item_2,B,test_1,73,50,80
item_2,B,test_2,107,75,67
item_2,B,test_3,119,100,50

输出data-out.csv

ID,Flow,TestNa,Label,Data
item_1,A,test_1,4,89
item_1,A,test_1,5,51
item_1,A,test_1,6,67
item_1,A,test_2,4,60
item_1,A,test_2,5,67
item_1,A,test_2,6,44
item_1,A,test_3,4,111
item_1,A,test_3,5,82
item_1,A,test_3,6,67
item_1,B,test_1,4,88
item_1,B,test_1,5,78
item_1,B,test_1,6,75
item_1,B,test_2,4,104
item_1,B,test_2,5,66
item_1,B,test_2,6,66
item_1,B,test_3,4,109
item_1,B,test_3,5,87
item_1,B,test_3,6,49
item_2,A,test_1,4,76
item_2,A,test_1,5,76
item_2,A,test_1,6,88
item_2,A,test_2,4,59
item_2,A,test_2,5,44
item_2,A,test_2,6,60
item_2,A,test_3,4,91
item_2,A,test_3,5,72
item_2,A,test_3,6,59
item_2,B,test_1,4,73
item_2,B,test_1,5,50
item_2,B,test_1,6,80
item_2,B,test_2,4,107
item_2,B,test_2,5,75
item_2,B,test_2,6,67
item_2,B,test_3,4,119
item_2,B,test_3,5,100
item_2,B,test_3,6,50

答案 1 :(得分:0)

Pandas的融化功能将完全符合您的要求:

import pandas as pd

#read in fixed width file
df = pd.read_fwf('input.csv',header=0)

#Melt the dataframe and sort as in the example provided.
df = pd.melt(df,id_vars=['ID','Flow','TestNa'],value_vars=['4','5','6'],var_name='Label', value_name='Data').sort(['ID','Flow','TestNa','Label'])

#output fixed width file
text_file = open("output.csv", "w")
text_file.write(df.to_string(index=None))
text_file.close()

在输出文件中给出以下内容:

     ID Flow  TestNa Label  Data
 item_1    A  test_1     4    89
 item_1    A  test_1     5    51
 item_1    A  test_1     6    67
 item_1    A  test_2     4    60
 item_1    A  test_2     5    67
 item_1    A  test_2     6    44
 item_1    A  test_3     4   111
 item_1    A  test_3     5    82
 item_1    A  test_3     6    67
 item_1    B  test_1     4    88
 item_1    B  test_1     5    78
 item_1    B  test_1     6    75
 item_1    B  test_2     4   104
 item_1    B  test_2     5    66
 item_1    B  test_2     6    66
 item_1    B  test_3     4   109
 item_1    B  test_3     5    87
 item_1    B  test_3     6    49
 item_2    A  test_1     4    76
 item_2    A  test_1     5    76
 item_2    A  test_1     6    88
 item_2    A  test_2     4    59
 item_2    A  test_2     5    44
 item_2    A  test_2     6    60
 item_2    A  test_3     4    91
 item_2    A  test_3     5    72
 item_2    A  test_3     6    59
 item_2    B  test_1     4    73
 item_2    B  test_1     5    50
 item_2    B  test_1     6    80
 item_2    B  test_2     4   107
 item_2    B  test_2     5    75
 item_2    B  test_2     6    67
 item_2    B  test_3     4   119
 item_2    B  test_3     5   100
 item_2    B  test_3     6    50

注意:我假设您正在使用基于示例表的固定宽度文件。如果您使用的是纯csv,则可以替换以下内容:

df = pd.read_csv('input.csv',header=0)

#Melt the dataframe and sort as in the example provided.
df = pd.melt(df,id_vars=['ID','Flow','TestNa'],value_vars=['4','5','6'],var_name='Label', value_name='Data').sort(['ID','Flow','TestNa','Label'])

df.to_csv('output.csv',index=None)
相关问题