I have been trying to get the frequency of each ID per day over a period of time. I have the following dataframe:
data1 = pd.DataFrame({
'Date_Time': [
'2010-01-01', '2010-01-01',
'2010-04-02', '2010-04-01',
'2011-01-01', '2011-01-01',
'2013-01-01', '2014-01-01',
'2014-01-01', '2015-01-01',
'2016-01-01', '2011-01-01'],
'ID': [1, 1, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6]
})
So I would like to get the frequency of each ID per day given that there are many days in which the same ID exists. I tried the following approach which worked partly and am still strugling with getting the it right. Here is the code which I have used:
for dt in set(data1['Date_Time']):
for id in df['ID']:
length = len(data1[data1['Date_Time']==dt])
data1.loc[data1['Date_Time']==dt, 'new'] = length
The final result should be looking something like this