Pandas groupby.agg count returns wrong count

79 Views Asked by At

I'm trying to count rows by 3 columns (county,year and month),

data is very simple

year month date county
2024 2 20240204 Taoyuan
2024 2 20240224 Chiayi
2024 1 20240118 Kaoshung
2024 2 20240213 Xinchu
2024 2 20240203 Taichung

but the code like below

output_data = input_data.groupby(["county","year","month"]).agg({'count': ['count']})

and this code

output_data = pd.DataFrame(input_data.groupby(["county","year","month"])['count'].count())

have different results.

The results of these two is like

county year month count
A 2023 1 106
B 2023 1 44
C 2023 1 117
D 2023 1 60
E 2023 1 80
county year month count
A 2023 1 106
B 2023 1 48
C 2023 1 118
D 2023 1 60
E 2023 1 83

Any ideas?

0

There are 0 best solutions below