2

first question, sorry if I missed it while searching for an answer.

I have a dataframe with a column called 'id', with each id appearing several times. I'm trying to add a column to this dataframe that basically represents the sequence of occurrences of each unique 'id'. For example:

id:      1, 1, 1, 2, 2, 3
count:   1, 2, 3, 1, 2, 1

Sorry for horrible formatting, I'm so exhausted at this point, any help is appreciated!!!

BENY
  • 317,841
  • 20
  • 164
  • 234
Armon
  • 113
  • 6
  • 1
    df['count']=df.groupby('id').cumcount()+1 – BENY Oct 11 '18 at 17:10
  • Thanks Wen, I actually just found that answer this morning, sorry for the duplicate guys! Here's the URL to the original that I found for those that are interested: https://stackoverflow.com/questions/28598140/pandas-incrementally-count-occurrences-in-a-column – Armon Oct 12 '18 at 07:36

0 Answers0