我是python的新手(真的是任何类型的编码)。所以,如果有点混乱,我很抱歉
我有一个像以下
的csv文件A B C D E F G H
14 BP1 BP1-19119308 OR1A1 19119308 chip-chip Hs578T human 11/23/09
15 BP1 BP1-19119308 PTPRE 19119308 chip-chip Hs578T human 11/23/09
16 BP1 BP1-19119308 SELE 19119308 chip-chip Hs578T human 11/23/09
17 BP1 BP1-19119308 TAC3 19119308 chip-chip Hs578T human 11/23/09
18 BP1 BP1-19119308 VEGFA 19119308 chip-chip Hs578T human 11/23/09
19 CHD7 CHD7-19251738 APOA1 19251738 chip-chip MESC mouse 11/23/09
20 CHD7 CHD7-19251738 ARHGAP26 19251738 chip-chip MESC mouse 11/23/09
我需要让它看起来像这样
BP1-19119308-chip-chip-Hs578T-human OR1A1 PTPRE SELE TAC3 VEGFA
CHD7-19251738-chip-chip-MESC-mouse APOA1 ARHGAP26
我确实在第一栏中使用此
来管理C-F-G-Himport csv
out = open ('test.csv','rt', encoding='utf8')
data = csv.reader(out)
output = csv.writer(out)
data = [row for row in data]
new_data = [[row[2]+'-'+row[5]+'-'+row[6] +'-'+ row[7], row[3]] for row in data]
print (new_data)
out = open('new_data.csv','wt')
output = csv.writer(out)
for row in new_data:
output.writerow(row)
out.close()
A B
BP1-19119308-chip-chip-Hs578T-human OR1A1
BP1-19119308-chip-chip-Hs578T-human PTPRE
BP1-19119308-chip-chip-Hs578T-human SELE
BP1-19119308-chip-chip-Hs578T-human TAC3
BP1-19119308-chip-chip-Hs578T-human VEGFA
CHD7-19251738-chip-chip-MESC-mouse APOA1
CHD7-19251738-chip-chip-MESC-mouse ARHGAP26
CHD7-19251738-chip-chip-MESC-mouse ATP11A
但现在我在A中有这些副本,我不知道如何删除它们并转置B中分配给这些重复项的所有值。
我再次尝试循环以将当前值与之前的值进行比较,我只是搞砸了整个事情。
有什么建议吗?
答案 0 :(得分:1)
您想使用字典。如果您正在进行进一步分析,请将聚合值保存在每个标识符的列表中。您的标识符字符串是一个键,在每个键下,您都有一个值列表。
new_keys = [row[2] + '-' + row[5] + '-' + row[6] + '-' + row[7] for row in data]
new_values = [row[3] for row in data]
aggregate_values = {} # An empty dictionary
# Iterate across the paired lists together
for key, value in zip(new_keys, new_values):
if key not in aggregate_values:
aggregate_values[key] = [value]
else:
aggregate_values[key].append(value)
# printed output
for key in aggregate_values:
print key + " " + " ".join(aggregate_values[key])
答案 1 :(得分:0)
一种解决方案是在对数据进行分组时使用字典:
import csv
out = open ('test.csv','rt', encoding='utf8')
data = csv.reader(out)
output = csv.writer(out)
data = [row for row in data]
new_data = [[row[2]+'-'+row[5]+'-'+row[6] +'-'+ row[7], row[3]] for row in data]
my_dict = {}
for row in new_data:
if row[0] in my_dict:
my_dict[row[0]] += " " + row[1]
else:
my_dict[row[0]] = row[1]
new_data = [[my_key,my_dict[my_key]] for my_key in my_dict]
print (new_data)
out = open('new_data.csv','wt')
output = csv.writer(out)
for row in new_data:
output.writerow(row)
out.close()
答案 2 :(得分:0)
从这里得到的明星:
test.txt
A B
BP1-19119308-chip-chip-Hs578T-human OR1A1
BP1-19119308-chip-chip-Hs578T-human PTPRE
BP1-19119308-chip-chip-Hs578T-human SELE
BP1-19119308-chip-chip-Hs578T-human TAC3
BP1-19119308-chip-chip-Hs578T-human VEGFA
CHD7-19251738-chip-chip-MESC-mouse APOA1
CHD7-19251738-chip-chip-MESC-mouse ARHGAP26
CHD7-19251738-chip-chip-MESC-mouse ATP11A
现在,您可以使用以下代码来创建所需的形状:
with open("test.txt") as f:
data = f.readlines()[1:]
mydata = [x.strip() for x in data]
final = {}
for x in mydata:
k, v = x.split()
if final.has_key(k):
l = final[k]
l.append(v)
else:
final[k] = [v]
for d in final:
print d, " ".join(final[d])
输出:
CHD7-19251738-chip-chip-MESC-mouse APOA1 ARHGAP26 ATP11A
BP1-19119308-chip-chip-Hs578T-human OR1A1 PTPRE SELE TAC3 VEGFA
如果需要,可以在此处将其写入文件。
答案 3 :(得分:0)
使用itertools.groupby
和operator.itemgetter
。在初始化new_data
和output
:
for k, g in itertools.groupby(new_data, operator.itemgetter(0)):
row = [k]
row.extend(map(g, operator.itemgetter(1)))
output.writerow(row)
完整的改进的(重构)代码可能如下所示:
import csv
import itertools
import operator
with open('test.csv','rt', encoding='utf8') as f_in:
inp = csv.reader(f_in)
new_data = (('-'.join(operator.itemgetter(2, 5, 6, 7)), row[3])
for row in inp)
with open('new_data.csv','wt') as f_out:
output = csv.writer(f_out)
for k, g in itertools.groupby(new_data, operator.itemgetter(0)):
row = [k]
row.extend(map(g, operator.itemgetter(1)))
output.writerow(row)