How to present in Jinja2 notation groupedby dataframe to html template in Django
In Django view I have function where I got dataframe from QuerySet.
def owner_observation_count(request):
form = ReportForm(request.GET)
context = {}
if form.is_valid():
organisation = form.cleaned_data['organisation']
department_for_report = form.cleaned_data['department_for_report']
employee_total = Observation.objects.values('owner__last_name','owner__first_name', 'date_created__year','date_created__month').annotate(owner_count_2=Count('owner_id'))
if organisation:
employee_total = employee_total.filter(users_organization=organisation)
if department_for_report:
employee_total = employee_total.filter(users_department=department_for_report)
employee_total_pd = pd.DataFrame.from_dict(employee_total)
employee_total_pd["Emploeey"] = employee_total_pd["owner__last_name"].str.cat(' '
+ employee_total_pd["owner__first_name"].str.get(0) + '.')
employee_total_pd = employee_total_pd.drop('owner__last_name', axis=1)
employee_total_pd = employee_total_pd.drop('owner__first_name', axis=1)
emp_columns = {'date_created__year': 'Year',
'date_created__month': 'Month',
'owner_count_2': 'Qty'}
employee_total_pd.rename(columns=emp_columns, inplace=True)
which gives the result
| Year | Month | Qty | Employee | |------|-------|-----|------------| | 2023 | 6 | 2 |Kefer A. | | 2023 | 9 | 1 |Napylov S. | | 2023 | 6 | 2 |Grosheva N. | | 2023 | 9 | 3 |Sapego G. | | 2023 | 8 | 3 |Danilin S. | | 2023 | 8 | 2 |Alekseeva L.| | 2023 | 8 | 2 |Smirnova E. | | 2023 | 7 | 1 |SHaripov R. | | 2023 | 9 | 14 |Lyalina L. | | 2024 | 8 | 2 |Husainov S. | | 2024 | 8 | 3 |Kachanova T.| | 2024 | 8 | 6 |Chistova V. | | 2024 | 6 | 1 |Vishnyakov M| | 2024 | 6 | 3 |Kachan A. | | 2024 | 8 | 4 |Krohmalyuk E| | 2024 | 8 | 2 |Safeev N. | | 2024 | 8 | 4 |Vorobeva N. |
then I grouped the dataframe
employee_ready_pd = pd.crosstab(employee_total_pd.Emploeey, [employee_total_pd.Year, employee_total_pd.Month],
values=employee_total_pd.Qty, aggfunc='sum', margins=True, margins_name="Итого").fillna(0)
and got this result
then I pass it to context
context.update({'emp_pd': employee_total_pd})
context['form'] = form
return render(request, 'observations/reports.html', context)
Exactly on this similar grouped way I would like to get it in HTML page. How to properly display it via Jinja2 tags?
{% for item in emp_pd %}
....
{% endfor %}
Thanks for advance!
