Search code examples
python-3.xpandasdataframepandas-groupbytransformation

groupby by an ID and count unique start dates by each ID. Cast back to dataframe. Pandas


I have a pandas dataframe:

pd.DataFrame({
    'student_id': ['5', '5', '5', '5', '2', '2'],
    'start_date': ['2020-11-11', '2020-11-11', '2020-11-11', '2020-12-05', '2020-11-25', '2020-11-25']
})

I want to group the df by 'student_id' and count how many of the same start dates occur for each row. Then I want to cast this back to the original dataframe. For example, I would create a new column called 'course_enroll_count'. The first three rows would display 3 because the student has 3 start dates of '2020-11-11'. Expected output:

pd.DataFrame({
    'student_id': ['5', '5', '5', '5', '2', '2'],
    'start_date': ['2020-11-11', '2020-11-11', '2020-11-11', '2020-12-05', '2020-11-25', '2020-11-25'],
    'course_enroll_count': [3, 3, 3, 1, 2, 2]
})

Solution

  • Try with transform

    df['new'] = df.groupby(['student_id','start_date'])['start_date'].transform('count')
    df
    Out[313]: 
      student_id  start_date  new
    0          5  2020-11-11    3
    1          5  2020-11-11    3
    2          5  2020-11-11    3
    3          5  2020-12-05    1
    4          2  2020-11-25    2
    5          2  2020-11-25    2