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
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.'])...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With