将 Pandas DataFrame 保存到 Django 模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37688054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:20:55  来源:igfitidea点击:

Saving a Pandas DataFrame to a Django Model

pythondjangopandas

提问by Johan

I have stock price data that is stored in a pandas DataFrame as shown below (actually it was in a panel, but I converted it to a DataFrame)

我的股票价格数据存储在 Pandas DataFrame 中,如下所示(实际上它在面板中,但我将其转换为 DataFrame)

        date  ticker  close       tsr
0 2013-03-28  abc     22.81  1.000439
1 2013-03-28  def     94.21  1.006947
2 2013-03-28  ghi     95.84  1.014180
3 2013-03-28  jkl     31.80  1.000000
4 2013-03-28  mno     32.10  1.003125
...many more rows

I want to save this in a Django model, which looks like this (matches the column names):

我想将它保存在一个 Django 模型中,它看起来像这样(匹配列名):

class HistoricalPrices(models.Model):
    ticker = models.CharField(max_length=10)
    date = models.DateField()
    tsr = models.DecimalField()
    close = models.DecimalField()

The best I've come up so far is using this to save it, where df is my DataFrame:

到目前为止,我想到的最好的方法是使用它来保存它,其中 df 是我的 DataFrame:

entries = []
for e in df.T.to_dict().values():
    entries.append(HistoricalPrices(**e))
HistoricalPrices.objects.bulk_create(entries)

Is there a better way to save this?

有没有更好的方法来保存它?

I've looked at django-pandas, but looks like it just reads from the DB.

我看过django-pandas,但看起来它只是从数据库中读取。

回答by Stefan

It would be most efficient to use to_sql()with appropriate connectionparameters for the engine, and run this inside your Djangoapp rather than iterating through the DataFrameand saving one modelinstance at a time:

使用to_sql(), 的适当connection参数engine并在您的Django应用程序中运行它是最有效的,而不是一次迭代DataFrame并保存一个model实例:

from django.conf import settings

user = settings.DATABASES['default']['USER']
password = settings.DATABASES['default']['PASSWORD']
database_name = settings.DATABASES['default']['NAME']

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)
df.to_sql(HistoricalPrices, con=engine)