I have a DataFrame:
`exp_no` 'time' 'price'
1 0:00:00 20.0
1 7 days 45.0
1 15 days 100.0
2 0:00:00 20.0
2 7 days 45.0
2 15 days 100.0
The corresponding Django model:
class StData(models.Model):
exp_no = models.ForeignKey(StIndex, on_delete=models.CASCADE)
time = models.DateTimeField()
price = models.DecimalField(max_digits=10, decimal_places=2)
I want to make a smaller table which would have exp_no, max_time and corresponding price such that:
`exp_no` 'time' 'price'
1 15 days 100.0
2 15 days 100.0
In pandas, I'd do df.groupby('exp_no')['time', 'price'].max().reset_index()
to get the desired table.
In Django ORM annotation to get the same result(or Queryset) if I do:
qs.values('exp_no').annotate(max_time=Max('time')).order_by()
it gives me exp_no and time, but I want to get the corresponding price as well. I have looked through this answer in SO: Django orm get latest for each group
But not sure how I get the price. Using Django 2.0 with sqlite3.
I appreciate any help.