3

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

Tmiskiewicz
  • 389
  • 1
  • 11

1 Answers1

2

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.'])...
johnjohn
  • 716
  • 2
  • 9
  • 17