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

1
  • Check second answer. Commented Mar 12, 2021 at 9:10

1 Answer 1

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.'])...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.