[英]How to export csv tables to python dictionary quickly?
我有一个 csv 表,其中包含我希望导出到 Python 字典中的化合物及其属性和描述:
上表应转换为以下字典:
compound_data = {COMPOUND 1:{'Property 1':'Description 1', 'Property 2':'Description 2', 'Property 3':'Description 3', 'Property n':'Description n'}, COMPOUND 2:{'Property 1':'Description 1', 'Property 2':'Description 2', 'Property x':'Description x'}, COMPOUND n:{'Property y':'Description y', 'Property z':'Description z'}}
表中有数百种化合物,每种都具有可变数量的特性。 化合物可能具有共同的特性(例如沸点),或者它们可能具有独特的特性(例如电导率)。 我写了一些访问复合信息的代码,如下所示:
compound_data['COMPOUND 1']['Property 1'] = 'Description 1'
将表格读入 python 字典的最简单、最 Pythonic 的方法是什么?
这应该做。
import csv
with open('csv_file.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
compound_dictionary = dict()
last_compound_key = str()
for row in csv_reader:
if row[0] != '':
last_compound_key = row[0]
compound_dictionary[last_compound_key] = dict()
compound_dictionary[last_compound_key][row[1]] = row[2]
print(compound_dictionary)
您可以在 Pandas 的帮助下轻松做到这一点。
import pandas as pd
# Read the csv file
df = pd.read_csv('check.csv')
# Fill the nan values with previous values in the first column
df['compound'] = df['compound'].fillna(method='ffill')
compounds = {}
# Iterate through the dataframe
for row in df.iterrows():
# Check if the key has already added to the temp dictionary. If available then append else add new one
if row[1]['compound'] in tmp.keys():
tmp[row[1]['compound']][row[1]['property']] = row[1]['value']
else:
tmp[row[1]['compound']] = {row[1]['property']: row[1]['value']}
# The result is available as a dictionary
print(compounds)
print(compounds['compound a']['property a'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.