Skip to content

Instantly share code, notes, and snippets.

@alphakilo7
Last active November 28, 2021 18:14
Show Gist options
  • Save alphakilo7/91f5d85796176018831fc4c2b2cd6ace to your computer and use it in GitHub Desktop.
Save alphakilo7/91f5d85796176018831fc4c2b2cd6ace to your computer and use it in GitHub Desktop.
import pandas as pd
from django.db.models.query import QuerySet
def qs_to_df(qs, columns=[]):
""" A utility function to convert any QuerySet to pandas.DataFrame
:params:
qs: QuerySet => A valid queryset of any django.model.Model
columns: list => An iterable that contains required columns
if not provided, the model fields will be taken into
consideration.
the `columns` may also contain the custom model methods and properties
the valid model methods/properties will be stored in the dataframe
:returns:
df: pd.DataFrame => A pandas DataFrame with required columns
"""
assert type(qs) is QuerySet, TypeError("a 'django.db.models.query.QuerySet' "
"must be passed for conversion, "
f"found '{qs.__class__.__name__}'")
assert qs, ValueError("cannot process empty QuerySet")
cols = set()
if columns:
for obj in qs:
for col in columns:
if not hasattr(obj, col):
raise AttributeError("object does not have attribute named: "
f"'{col}'")
else:
for obj in qs:
cols.update({col for col in vars(obj) if not col.startswith("_")})
columns = sorted(list(cols))
df_data = []
for obj in qs:
row = []
for col in columns:
if callable(getattr(obj, col)):
row.append(getattr(obj, col)())
else:
row.append(getattr(obj, col))
df_data.append(row)
df = pd.DataFrame(df_data, columns=columns)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment