如何将Pandas Dataframe写入Django模型 [英] How to write a Pandas Dataframe to Django model

查看:4185
本文介绍了如何将Pandas Dataframe写入Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用大熊猫的python,我通常写数据框到我的数据库表如下。我现在正在迁移到Django,如何通过名为MyModel的模型将相同的数据框写入表中?帮助真的很感激。

 #原始大熊猫代码
engine = create_engine('postgresql:// myuser:mypassword @ localhost :5432 / mydb',echo = False)
mydataframe.to_sql('mytable',引擎,if_exists ='append',index = True)


解决方案

使用您自己的熊猫代码沿着映射到同一SQL表的Django模型



我不知道有没有明确的支持将大熊猫数据框写入Django模型。然而,在Django应用程序中,除了使用ORM(例如通过Django模型)之外,您仍然可以使用自己的代码读取或写入数据库。



鉴于您最有可能在以前由pandas编写的数据库中的数据,您可以继续使用相同的数据库和相同的熊猫代码,只需创建一个可以访问该表的Django模型



eg如果您的大熊猫代码正在写入SQL表 mytable ,只需创建一个如下所示的模型:

 code> class MyModel(Model):
class Meta:
db_table ='mytable'#这告诉Django SQL表是
managed = False#如果表已经存在
#,不需要由Django

field_1 = ...
field_2 = ...

现在您可以使用Django的模型与现有的熊猫代码(可能在单个Django应用程序中)同时使用。



Django数据库设置



要获取相同的DB凭据进入大熊猫SQL函数只需从Django设置中读取字段,例如:

 从django.conf导入设置

user = settings.DATABASES ['default'] ['USER']
密码= settings.DATABASES ['default'] ['PASSWORD']
database_name = settings.DATABASES [默认'] ['NAME']
#host = settings.DATABASES ['default'] ['HOST']
#port = settings.DATABASES ['default'] ['PORT']

database_url ='postgresql:// {user}:{password} @localhost:5432 / {database_name}'.format(
user = user,
password = password,
database_name = database_name,


engine = create_engine(database_url,echo = False)



不推荐使用替代方法,因为它的效率不高



我并没有真正看到排列方式阅读数据框,然后创建一个模型实例,并保存它,这是非常慢的。你可能会有一些批量插入操作,但为什么麻烦,因为熊猫'$ code> to_sql 已经为我们这样做了。将熊猫数据集读入大熊猫资料框中效果不佳,大熊猫可以为我们做得更快。

 #这样做对于索引缓慢的
,df.iterrows()中的行:
model = MyModel()
model.field_1 = row ['field_1']
model.save()


I have been using pandas in python and I usually write a dataframe to my db table as below. I am now now migrating to Django, how can I write the same dataframe to a table through a model called MyModel? Assistance really appreciated.

# Original pandas code
    engine = create_engine('postgresql://myuser:mypassword@localhost:5432/mydb', echo=False)
    mydataframe.to_sql('mytable', engine,if_exists='append',index=True)

解决方案

Use your own pandas code along side a Django model that is mapped to the same SQL table

I am not aware of any explicit support to write a pandas dataframe to a Django model. However, in a Django app, you can still use your own code to read or write to the database, in addition to using the ORM (e.g. through your Django model)

And given that you most likely have data in the database previously written by pandas' to_sql, you can keep using the same database and the same pandas code and simply create a Django model that can access that table

e.g. if your pandas code was writing to SQL table mytable, simply create a model like this:

class MyModel(Model):
    class Meta:
        db_table = 'mytable' # This tells Django where the SQL table is
        managed = False # Use this if table already exists
                        # and doesn't need to be managed by Django

    field_1 = ...
    field_2 = ...

Now you can use this model from Django simultaneously with your existing pandas code (possibly in a single Django app)

Django database settings

To get the same DB credentials into the pandas SQL functions simply read the fields from Django settings, e.g.:

from django.conf import settings

user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']
# host = settings.DATABASES['default']['HOST']
# port = settings.DATABASES['default']['PORT']

database_url = 'postgresql://{user}:{password}@localhost:5432/{database_name}'.format(
    user=user,
    password=password,
    database_name=database_name,
)

engine = create_engine(database_url, echo=False)

The alternative is not recommended as it's inefficient

I don't really see a way beside reading the dataframe row by row and then creating a model instance, and saving it, which is really slow. You might get away with some batch insert operation, but why bother since pandas' to_sql already does that for us. And reading Django querysets into a pandas dataframe is just inefficient when pandas can do that faster for us too.

# Doing it like this is slow
for index, row in df.iterrows():
     model = MyModel()
     model.field_1 = row['field_1']
     model.save()

这篇关于如何将Pandas Dataframe写入Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆