我有 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
有任何想法吗?提前致谢。