0
我有2個CSV文件。創建字典,只添加一列與列表中的值匹配的行
首先,我想取1列並列出一個列表。
然後,我想從另一個CSV創建一個字典,但僅限於其中一列的值與先前創建的列表中的值匹配的行。
這裏是到目前爲止的代碼:
#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
這裏有2個樣本數據文件:http://bit.ly/1hlpyTH
任何想法?提前致謝。