Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count frequency by data in dataframe? [duplicate]

I have an issue with groupby in pandsa. My DF looks like below:

time    ID
01-13   1
01-13   2
01-14   3
01-15   4
01-15   5

I need result like below:

time   ID
01-13  2
01-14  1
01-15  2

So basically I need to count frequency ID by data. I treid with this but I am not sure of result (df is huge). Any idea? Thanks for help

df = df.groupby("Time").Id.value_counts()

Best regards

like image 752
Tmiskiewicz Avatar asked Feb 25 '26 09:02

Tmiskiewicz


1 Answers

One way is:

df.time.value_counts()

Output:

01-15    2
01-13    2
01-14    1
Name: time, dtype: int64

Other way, as suggested by reviewer above:

df.groupby(['time']).size().reset_index(name='Frequency')

Output:

    time    Frequency
0   01-13   2
1   01-14   1
2   01-15   2

Note you can group by several variables if needed:

df.groupby(['col1', 'col2', 'etc.'])...
like image 114
johnjohn Avatar answered Mar 03 '26 16:03

johnjohn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!