答案 0 :(得分:1)
Anzel回答了迭代问题here - 使用the Pandas to_csv() function。我的字典语法也错了。因此,我的最终解决方案是:
# populate a Dictionary to find dimension_ids for category labels
parent_dimension_age = Dimension.objects.get(name='Age')
parent_dimension_income = Dimension.objects.get(name='Income')
dims_age = dict([ (d.name, d.id) for d in Dimension.objects.filter(parent_id=parent_dimension_age.id) ])
dims_income = dict([ (d.name, d.id) for d in Dimension.objects.filter(parent_id=parent_dimension_income.id) ])
# Retrieves a row at a time into a comma delimited string
for line in pandas_pivottable.to_csv(header=False, index=True, sep='\t').split('\n'):
if line:
# row[0] = income category, row[1] = age category, row[2] = age, row[3] = income
row = line.split('\t')
entity = Entity(name='data pivot row', dataset_id=dataset.id)
entity.save()
# dims_age.get(row[1]) gets the ID for the category whose name matches the contents of row[1]
age_val = FloatValue(value=row[2], entity_id=entity.id, attribute_id=attrib_age.id, dimension_id=dims_age.get(row[1]))
age_val.save()
income_val = FloatValue(value=row[3], entity_id=entity.id, attribute_id=attrib_income.id, dimension_id=dims_income.get(row[0]))
income_val.save()
有关实体 - 属性 - 值(EAV)架构的更多信息,请参阅the Wikipedia page,(如果您正在考虑the Django-EAV extension)。但是,在本项目的下一次迭代中,我将用postgresql's new JSONB type替换它。这有望使数据更清晰,表现同等或更好。