Task:
Calculate the frequency of each ID for each month of 2021
- Frequency formula: Count of dates within each month for each ID (e.g. ID: 44; Month: 01-2021; Frequency: 3)
Sample:
times = [
'2021-02-18',
'2021-03-02',
'2021-03-02',
'2021-04-23',
'2021-01-18',
'2021-01-19',
'2021-01-20',
'2021-01-03',
'2021-02-04',
'2021-02-04'
]
id = [1, 1, 1, 1, 44, 44, 44, 46, 46, 46]
df = pd.DataFrame({'ID':id, 'Date': pd.to_datetime(times)})
df = df.reset_index(drop=True)
print(df)
ID Date
0 1 2021-02-18
1 1 2021-03-02
2 1 2021-03-02
3 1 2021-04-23
4 44 2021-01-18
5 44 2021-01-19
6 44 2021-01-20
7 46 2021-01-03
8 46 2021-02-04
9 46 2021-02-04
Desired Output:
id 01_2021 02_2021 03_2021 04_2021
0 1 0 1 2 1
1 44 3 0 0 0
2 46 1 2 0 0