Используя pandas и следующий код, я присвоил каждой группе промежуточный итог «Итого»:
Код: Выделить всё
bin_df = df[df["category"].isin(model.BINARY_CATEGORY_VALUES)]
bin_category_mime_type_count_df = (
bin_df.groupby(["category", "mime_type"])["mime_type"]
.count()
.reset_index(name="Count")
)
Код: Выделить всё
category mime_type Count
1 application/x-executable 19
1 application/x-pie-executable 395
1 application/x-sharedlib 1
2 application/x-sharedlib 755
3 application/x-sharedlib 1
6 application/x-object 129
Код: Выделить всё
bin_category_total_count_df = (
bin_category_mime_type_count_df.groupby(["category", "mime_type"])[
"Count"
]
.sum()
.unstack()
)
bin_category_total_count_df = (
bin_category_total_count_df.assign(
Total=bin_category_total_count_df.sum(1)
)
.stack()
.to_frame("Count")
)
bin_category_total_count_df["Count"] = (
bin_category_total_count_df["Count"].astype("Int64").fillna(0)
)
Код: Выделить всё
Count
category mime_type
1 application/x-executable 19
application/x-pie-executable 395
application/x-sharedlib 1
Total 415
2 application/x-sharedlib 755
Total 755
3 application/x-sharedlib 1
Total 1
6 application/x-object 129
Total 129
Код: Выделить всё
Count
category mime_type
2 application/x-sharedlib 755
Total 755
1 application/x-pie-executable 395
application/x-executable 19
application/x-sharedlib 1
Total 415
6 application/x-object 129
Total 129
3 application/x-sharedlib 1
Total 1
Подробнее здесь: https://stackoverflow.com/questions/787 ... -and-count