I have a dataframe that looks like this:
df = pd.DataFrame( {'ID':range(1,366), 'No_of_Visits': np.random.randint(1,100, 365), 'Date':pd.date_range(pd.datetime.today(), periods=365).tolist() })
I want to count the number of visits for each three months, and return the result as a column to look like this:
df = ID, No_of_Visits, Date, Count_first _3_Month, Count_Second_3_Months, Count_third_3_Months, Count_forth_3_Months
Here is what I tried:
My idea is to extract the months so I did this
df['Month'] = df['Date'].dt.month
and then groupby month and sum:
df['monthly_count'] = df.groupby(['Month'])['No_of_Visits'].transform('sum')
I now got stuck becuase:
1- I wanted to have a parameter that controls for how many month can I count the number of visits (e.g 3 month, 6 month, etc)
2- how to return these counts and store them in new columns?
Any hints?