How do I convert a django QuerySet to numpy record array?
How do I convert a django QuerySet to numpy record array?
How do I convert a django QuerySet to numpy record array?
PS: I know you can iterate and construct it and but is there any other cleaner solution?
Answer by ozan for How do I convert a django QuerySet to numpy record array?
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.
Answer by Vishal for How do I convert a django QuerySet to numpy record array?
What I was looking for:
From QuerySet qs get vlqs (django.db.models.query.ValuesListQuerySet)
vlqs = qs.values_list()
Covert vlqs to list
mylist = list(vlqs)
Create numpy record array
r = np.core.records.array(mylist, names='field1, field2, field3') // Names are the model fields
Answer by nealmcb for How do I convert a django QuerySet to numpy record array?
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 Django get a model's fields
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_'))
Answer by rodrixd for How do I convert a django QuerySet to numpy record array?
what you could do is:
[index[0] for index in qs.values_list('votes')]
and ready...XD
Answer by Umut Gunebakan for How do I convert a django QuerySet to numpy record array?
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).
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71
0 comments:
Post a Comment