用数组转置

时间:2018-06-28 22:54:12

标签: arrays sas

我需要使用数组将宽 "list": [ { "1": "2" //37 }, { "2": "1" //45 }, { "3": "4" //12 }, { "4": "3" //21 }, { "5": "3" //21 } ], 转换为窄dataset,但是我不确定如何。宽dataset看起来像这样:

id name t1 t2 t3 t4 t5 t6
11 Jack 79 83 87 70 98 161
12 John 89 73 87 99 100 170
13 Juan 99 93 93 99 99 186

我需要输出看起来像这样:

11 Jack F1 79
11 Jack F2 83
11 Jack F3 87
11 Jack F4 70
11 Jack Q1 98
11 Jack G1 161
12 John F1 89
12 John F2 73
12 John F3 87
12 John F4 99
12 John Q1 100
12 John G1 170
13 Juan F1 99
13 Juan F2 93
13 Juan F3 93
13 Juan F4 99
13 Juan Q1 99
13 Juan G1 186

数组超出了我的专业知识,因此我需要一些帮助。

1 个答案:

答案 0 :(得分:0)

您不需要数组来转置此数据。您可以在传入和传出的数据上使用import csv Col1 = "Product Name" Col2 = "Product Image Is Thumbnail - 1" Col3 = "Product Code/SKU" Col4 = "Product Description" Col5 = "Price" Col6 = "Cost Price" Col7 = "Product Image File - 1" Col8 = "Product Image File - 2" mydictionary={Col1:[], Col2:[], Col3:[], Col4 :[], Col5:[], Col6:[], Col7:[], Col8:[]} csvFile = csv.reader(open("wedding.csv")) for row in csvFile: mydictionary[Col1].append(row[2]) mydictionary[Col2].append(row[6]) mydictionary[Col3].append(row[4]) mydictionary[Col4].append(row[11]) mydictionary[Col5].append(row[7]) mydictionary[Col6].append(row[8]) mydictionary[Col7].append(row[9]) mydictionary[Col8].append(row[10]) print (mydictionary) with open('test.csv', 'w') as csv_file1: writer = csv.writer(csv_file1, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) for key, value in mydictionary.items(): writer.writerow([key, value])`` Proc TRANSPOSE数据集选项。

数据

rename=

转置

data have; input
id name $ t1 t2 t3 t4 t5 t6; datalines;
11 Jack 79 83 87 70 98 161
12 John 89 73 87 99 100 170
13 Juan 99 93 93 99 99 186
run;