我需要从 CSV 文件创建一个表。我想我可以用不同的库来做到这一点,但在这种情况下,我选择使用pandas,因为在不久的将来我会更需要它来进行一些数据分析。我有一个脚本,但出现此错误:Traceback (most recent call last): File "/home/gonzales/Escritorio/virtual_envs/stickers_gallito_env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc return self._engine.get_loc(key) File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 964, in pandas._libs.hashtable.Int64HashTable.get_itemKeyError: 1867Dropbox 中的数据:https://www.dropbox.com/s/o3iga509qi8suu9/ubigeo-peru-2018-12-25.csv?dl=0脚本:import pandas as pdimport csvfrom shop.models import Perufrom django.core.management.base import BaseCommandtmp_data=pd.read_csv('static/data/ubigeo-peru-2018-12-25.csv',sep=',', encoding="utf-8")class Command(BaseCommand): def handle(self, **options): products = [ Peru( departamento=tmp_data.ix[row]['departamento'], provincia=tmp_data.ix[row]['provincia'], distrito=tmp_data.ix[row]['distrito'], ) for row in tmp_data['id'] ] Peru.objects.bulk_create(products)模型.pyclass Peru(models.Model): departamento = models.CharField(max_length=100, blank=False) provincia = models.CharField(max_length=100, blank=False) distrito = models.CharField(max_length=100, blank=False) def __str__(self): return self.departamento
2 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
这不起作用(并为最后一个对象引发错误)的原因row实际上id是您的数据在您将其用作索引时从 1 开始。
像这样使用它:
products = [
Peru(
departamento=tmp_data.ix[row-1]['departamento'],
provincia=tmp_data.ix[row-1]['provincia'],
distrito=tmp_data.ix[row-1]['distrito'],
)
for row in tmp_data['id']
]
或者您可以像库推荐的那样迭代数据帧:
products = []
for i, row in tmp_data.iterrows():
products.append(Peru(
departamento=row]['departamento'],
provincia=row['provincia'],
distrito=row['distrito'],
))
Peru.objects.bulk_create(products)
添加回答
举报
0/150
提交
取消