pandas dataframe作为django中的字段

时间:2022-08-22 23:44:29

I want to add pandas dataframe (or a numpy array) as a field in django model. Each model instance in django has a large size 2D array associated with it so I want to store it as numpy array or pandas dataframe.

我想在django模型中添加pandas dataframe(或numpy数组)作为字段。 django中的每个模型实例都有一个与之关联的大尺寸2D数组,因此我想将其存储为numpy数组或pandas数据帧。

please guide me how can I achieve it. I tried below but it doesn't work.

请指导我如何实现它。我在下面试过,但它不起作用。

from django.db import models
import numpy    
class datafile(models.Model)
    filename = models.CharField(max_length=50)
    data = numpy.array((1000,1000))

I am reading data from excel file and setting it to data variable in views.py but after I save the model, the value for data doesn't get updated. regards, Rahul

我正在从excel文件中读取数据并将其设置为views.py中的数据变量,但在保存模型后,数据的值不会更新。问候,拉胡尔

3 个解决方案

#1


8  

You're likely gonna want to use a PickleField to store your numpy or Pandas dataframe. The PickleField will serialize the data into a text field for the database storage and convert it back upon retrieval.

您可能想要使用PickleField来存储您的numpy或Pandas数据帧。 PickleField会将数据序列化为数据库存储的文本字段,并在检索时将其转换回来。

from django.db import models
from picklefield.fields import PickledObjectField
import numpy

class DatafileModel(models.Model)
    data = PickledObjectField()

And then just create or manipulate your model instances with numpy or pandas objects:

然后使用numpy或pandas对象创建或操作模型实例:

datafile = DatafileModel()
datafile.data = numpy.array((1000,1000))
datafile.save()

#2


1  

Late to the discussion here, but if you're using PostgreSQL you could leverage it's JSON support via django_pgjsonb and convert back-and-forth between the DataFrame and JSON representations of the content.

这里讨论的后期,但如果您正在使用PostgreSQL,您可以通过django_pgjsonb利用它的JSON支持,并在内容的DataFrame和JSON表示之间来回转换。

Something like this should work (you'll need to play with the 'orient' parameter):

这样的东西应该工作(你需要使用'orient'参数):

import pandas as pd
from django_pgjsonb import JSONField


class MyModel(models.Model):
    dfjson = JSONField()

    @property
    def store_dataframe(self, dataframe):
        self.dfjson = dataframe.to_json()

    @property
    def load_dataframe(self):
        return pandas.read_json(self.json)    

my_instance = MyModel()
my_instance.store_dataframe(df)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
my_instance.store_dataframe(df)
my_instance.save()
new_df = my_instance.load_dataframe()
#hopefully new_df and df are equivalent

https://github.com/yjmade/django-pgjsonb
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html

https://github.com/yjmade/django-pgjsonb http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html http://pandas.pydata.org/pandas-docs/稳定/生成/ pandas.DataFrame.to_json.html

#3


0  

In order not to create a necessity to create model instances separately one could use the following simple approach

为了不创建单独创建模型实例的必要性,可以使用以下简单方法


import pandas as pd
from django.contrib.postgres.fields import JSONField

class StoredDataFrame(Model):
    data = JSONField()

    @classmethod
    def putframe(cls, dataframe):
        storeddataframe = cls(data=dataframe.to_json(orient='split'))
        storeddataframe.save()
        return storeddataframe

    def loadframe(self):
        return pd.read_json(self.data, orient='split')

Sample use:


    df = pd.DataFrame(np.random.randn(6,4), index=list('qwerty'), columns=list('ABCD'))
    storeddata = StoredDataFrame.putframe(df)
    retrieveddataframe = storeddata.loadframe()

#1


8  

You're likely gonna want to use a PickleField to store your numpy or Pandas dataframe. The PickleField will serialize the data into a text field for the database storage and convert it back upon retrieval.

您可能想要使用PickleField来存储您的numpy或Pandas数据帧。 PickleField会将数据序列化为数据库存储的文本字段,并在检索时将其转换回来。

from django.db import models
from picklefield.fields import PickledObjectField
import numpy

class DatafileModel(models.Model)
    data = PickledObjectField()

And then just create or manipulate your model instances with numpy or pandas objects:

然后使用numpy或pandas对象创建或操作模型实例:

datafile = DatafileModel()
datafile.data = numpy.array((1000,1000))
datafile.save()

#2


1  

Late to the discussion here, but if you're using PostgreSQL you could leverage it's JSON support via django_pgjsonb and convert back-and-forth between the DataFrame and JSON representations of the content.

这里讨论的后期,但如果您正在使用PostgreSQL,您可以通过django_pgjsonb利用它的JSON支持,并在内容的DataFrame和JSON表示之间来回转换。

Something like this should work (you'll need to play with the 'orient' parameter):

这样的东西应该工作(你需要使用'orient'参数):

import pandas as pd
from django_pgjsonb import JSONField


class MyModel(models.Model):
    dfjson = JSONField()

    @property
    def store_dataframe(self, dataframe):
        self.dfjson = dataframe.to_json()

    @property
    def load_dataframe(self):
        return pandas.read_json(self.json)    

my_instance = MyModel()
my_instance.store_dataframe(df)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
my_instance.store_dataframe(df)
my_instance.save()
new_df = my_instance.load_dataframe()
#hopefully new_df and df are equivalent

https://github.com/yjmade/django-pgjsonb
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html

https://github.com/yjmade/django-pgjsonb http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html http://pandas.pydata.org/pandas-docs/稳定/生成/ pandas.DataFrame.to_json.html

#3


0  

In order not to create a necessity to create model instances separately one could use the following simple approach

为了不创建单独创建模型实例的必要性,可以使用以下简单方法


import pandas as pd
from django.contrib.postgres.fields import JSONField

class StoredDataFrame(Model):
    data = JSONField()

    @classmethod
    def putframe(cls, dataframe):
        storeddataframe = cls(data=dataframe.to_json(orient='split'))
        storeddataframe.save()
        return storeddataframe

    def loadframe(self):
        return pd.read_json(self.data, orient='split')

Sample use:


    df = pd.DataFrame(np.random.randn(6,4), index=list('qwerty'), columns=list('ABCD'))
    storeddata = StoredDataFrame.putframe(df)
    retrieveddataframe = storeddata.loadframe()