2

I need to run a query like this - historic_data.objects.raw("select * from company_historic_data")

This return a RawQuerySet. I have to convert values from this to a dataframe. the usual .values() method does not work with raw query. Can someone suggest a solution.

Janhavi
  • 21
  • 2
  • 1
    Possible duplicate of [how to retrive values form RawQuerySet in django?](https://stackoverflow.com/questions/20325168/how-to-retrive-values-form-rawqueryset-in-django) – Gasanov May 10 '19 at 02:43

1 Answers1

4

Try the codes below

import pandas as pd

res = model.objects.raw('select * from some_table;')
df = pd.DataFrame([item.__dict__ for item in res])

Note that there is a _state column in the returned dataframe

Sway Jan
  • 321
  • 2
  • 8