0

My dataframe looks like this:

ID          Date
1001        2020-01-01   
1001        2020-02-01
1002        2020-01-01
1002        2020-02-01
1002        2020-03-01

I wanted to create a df with unique ID values and how many times that ID was repeated along with the date. I tried the following code:

df.groupby(['ID','Date'])['ID'].count()

The output I got was:

ID        Date
1001      2020-01-01       1
          2020-02-01       1
1002      2020-01-01       1
          2020-02-01       1
          2020-03-01       1

But what I want to create is this, i.e. column name 'Count' and should be in df format:

ID        Date             Count
1001      2020-01-01       1
          2020-02-01       2
1002      2020-01-01       1
          2020-02-01       2
          2020-03-01       3

The output is very close and only a slight tweaking is required. Can someone help me with this?

1 Answers1

0

Try this:

df['count'] = df.groupby(by=['ID']).cumcount()+1
print(df)

     ID        Date  count
0  1001  2020-01-01      1
1  1001  2020-02-01      2
2  1002  2020-01-01      1
3  1002  2020-02-01      2
4  1002  2020-03-01      3
NYC Coder
  • 7,424
  • 2
  • 11
  • 24