Pandas dataframe as a field in django

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

please call me how can I achieve this. I tried below, but that will not work.

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

I read the data from the excel file and set it to the data variable in views.py, but after saving the model, the value for the data is not updated. Regards, Rahul

+4
source share
3 answers

, PickleField - numpy Pandas. PickleField .

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

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

numpy Pandas :

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

, PostgreSQL, JSON django_pgjsonb DataFrame JSON.

- ( "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

+1

To avoid creating the need to instantiate the model separately, you can 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')

Using an example:


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

All Articles