2014-02-15 8 views
0

2 개의 CSV 파일이 있습니다.사전을 만들고 목록의 값과 일치하는 행만 추가하십시오.

먼저 1 열을 받아 목록을 만들고 싶습니다.

다른 CSV 사전을 만들고 싶습니다. 그러나 한 열의 값이 이전에 작성한 목록의 값과 일치하는 행만 사용해야합니다.

다음 코드는 지금까지의 : http://bit.ly/1hlpyTH

어떤 아이디어 : 여기

#modified from: http://bit.ly/1iOS7Gu 
import pandas 
colnames = ['Gene > DB identifier', 'Gene_Symbol', 'Gene > Organism > Name', 'Gene > Homologues > Homologue > DB identifier', 'Homo_Symbol', 'Gene > Homologues > Homologue > Organism > Name', 'Gene > Homologues > Data', 'Sets > Name'] 
data = pandas.read_csv(raw_input("Enter csv file (including path)"), names=colnames) 

filter = set(data.Homo_Symbol.values) 

print set(data.Homo_Symbol.values) 

#new_dict = raw_input("Enter Dictionary Name") 
#source: http://bit.ly/1iOS0e3 
import csv 
new_dict = {} 
with open('C:\Users\Chris\Desktop\gwascatalog.csv', 'rb') as f: 
    reader = csv.reader(f) 
    for row in reader: 
     if row[0] in filter: 
     if row[0] in new_dict: 
      new_dict[row[0]].append(row[1:]) 
     else: 
      new_dict[row[0]] = [row[1:]] 
print new_dict 

는이 샘플 데이터 파일입니다? 미리 감사드립니다. 당신은 collections.defaultdict을 사용할 수 있습니다

답변

1

은 DICT에 목록을 확인 없애 :

from collections import defaultdict 

new_dict = defaultdict(list) 
#... 
    for row in reader: 
     if row[0] in filter: 
     new_dict[row[0]].append(row[1:])