Starting from your Dataframe
:
>>> import pandas as pd
>>> df = pd.DataFrame({'Student_id': [1001, 1001, 1001, 1001, 1001, 1001, 1002, 1002, 1002],
... 'activity_timestamp': ['2019-09-05 08:26:12', '2019-09-06 09:26:12', '2019-09-21 10:11:01', '2019-10-24 11:44:01', '2019-10-25 11:31:01', '2019-10-26 12:13:01', '2019-09-11 12:21:01', '2019-09-12 13:11:01', '2019-11-23 16:22:01']},
... index = [0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> df
Student_id activity_timestamp
0 1001 2019-09-05 08:26:12
1 1001 2019-09-06 09:26:12
2 1001 2019-09-21 10:11:01
3 1001 2019-10-24 11:44:01
4 1001 2019-10-25 11:31:01
5 1001 2019-10-26 12:13:01
6 1002 2019-09-11 12:21:01
7 1002 2019-09-12 13:11:01
8 1002 2019-11-23 16:22:01
We convert the activity_timestamp
to datetime
and we extract the date and the month number like so :
>>> df['activity_timestamp'] = pd.to_datetime(df['activity_timestamp'], format='%Y-%m-%d %H:%M:%S.%f')
>>> df['date'] = df['activity_timestamp'].dt.date
>>> df['month'] = df['activity_timestamp'].dt.month_name()
>>> df
Student_id activity_timestamp date month
0 1001 2019-09-05 08:26:12 2019-09-05 September
1 1001 2019-09-05 08:26:13 2019-09-05 September
2 1001 2019-09-06 09:26:12 2019-09-06 September
3 1001 2019-09-21 10:11:01 2019-09-21 September
4 1001 2019-10-24 11:44:01 2019-10-24 October
5 1001 2019-10-25 11:31:01 2019-10-25 October
6 1001 2019-10-26 12:13:01 2019-10-26 October
7 1002 2019-09-11 12:21:01 2019-09-11 September
8 1002 2019-09-12 13:11:01 2019-09-12 September
9 1002 2019-11-23 16:22:01 2019-11-23 November
Then, we use the pivot_table()
method with the nunique
function instead of count
to get the number of unique date :
>>> df_result = (df.pivot_table(index='Student_id',
... columns='month',
... values='date',
... aggfunc=pd.Series.nunique,
... fill_value=0).rename_axis(columns=None)).add_prefix('total_active_days_in_').reset_index(drop=False)
>>> df_result
Student_id total_active_days_in_November total_active_days_in_October total_active_days_in_September
0 1001 0 3 3
1 1002 1 0 2
Thanks to @SeaBean for the add_prefix
method.