pandas count values for last 7 days from each date
The main obstacle is figuring out whether a date is within the last 7 days of the month. I'd recommend something hacky like the following:

from datetime import datetime, date, timedelta
def last7(datestr):
    orig = datetime.strptime(datestr,'%Y-%m-%d')
    plus7 = orig+timedelta(7)
    return plus7.month != orig.month


Once you have that, it's relatively simple to adapt your previous code:

df3 = df1[df1['is_buy'] == 1 &amp;&amp; last7(df1['date'])].groupby(['id', 'month']).agg({'is_buy': np.sum})


Now we just join together df2 and df3 and we're done.