2👍
What I was looking for:
-
From
QuerySet
qs
getvlqs
(django.db.models.query.ValuesListQuerySet
)vlqs = qs.values_list()
-
Covert
vlqs
to listmylist = list(vlqs)
-
Create numpy record array
# Names are the model fields r = np.core.records.array(mylist, names='field1, field2, field3')
23👍
import numpy as np
qs = MyModel.objects.all()
vlqs = qs.values_list()
r = np.core.records.fromrecords(vlqs, names=[f.name for f in MyModel._meta.fields])
This uses the QuerySet iterator directly and avoids the time-and-garbage-wasting step of creating a python list. It also uses MyModel._meta.fields to get the actual field names from the model, as explained at Get model's fields in Django
If you just want a single field (e.g. the ‘votes’ field of the model) extracted into a one-dimensional array, you can do:
vlqs = qs.values_list('votes', flat=True)
votes = np.fromiter(vlqs, numpy.dtype('int_'))
4👍
This is like asking “how do I convert the contents of my fridge into dinner?”. It depends on what you have in your fridge and what you’d like to eat. The short answer (equivalent to saying “by cooking”) is to iterate over the queryset, constructing objects of whatever composite data types you’d like to instantiate the array with (generally an iterable and a dictionary). The long answer depends on what you’d actually like to accomplish.
3👍
If you want to get all of your objects and create a numpy array with objects as elements of array:
import numpy as np
qs = MyModel.objects.all()
numpy_array = np.array(list(qs))
According to my work, I use something as below:
import numpy as np
qs = MyModel.objects.values_list('id','first_name','last_name').filter(gender='male').order_by('id')
numpy_array = np.array(list(qs))
Rows of array corresponds to records and columns of array corresponds to values that I defined above (id, first name, last name).
- Django multi-table inheritance, how to know which is the child class of a model?
- Django Rest Framework Nested Serializers
1👍
And to put it into a neat little function to which you just pass any Django Queryset:
import pandas as pd
import numpy as np
def qs_to_df(qs):
""" QuerySet to DataFrame """
Model = qs.model
np_array = np.core.records.fromrecords(qs.values_list(), names=[f.name for f in Model._meta.fields])
return pd.DataFrame(np_array)
- Django AttributeError: 'DatabaseOperations' object has no attribute 'select'
- Django REST Framework Swagger 2.0
0👍
Going off of @CpILL ‘s answer you can turn most querysets into a numpy record array like so:
def qs_to_ra(qs, *args):
"""
Turn most querysets directly into a numpy record array
:param qs: django queryset
:param args: takes a list of field names to specify
:return: numpy.recarray
"""
model = qs.model
if args:
return np.core.records.fromrecords(qs.values_list(*args), names=args)
return np.core.records.fromrecords(qs.values_list(), names=[f.name for f in model._meta.fields])
You can also turn them directly into a pandas dataframe like so:
def qs_to_df(qs, *args):
"""
Turn most querysets directly into a pandas dataframe.
:param qs: django queryset
:param args: takes a list of field names to specify
:return: pandas.DataFrame
"""
model = qs.model
if args:
return pd.DataFrame.from_records(list(qs.values_list(*args)), columns=args)
return pd.DataFrame.from_records(list(qs.values_list()), columns=[f.name for f in model._meta.fields])
- Does Django have a way to open a HTTP long poll connection?
- ForeignKey to a model that is defined after/below the current model
- How to emit SocketIO event on the serverside
- How to create a 8 digit Unique ID in Python?
- Uncaught ReferenceError: django is not defined