title: Find The Moving Average for Amount of Last 30 Days for each user Pandas Dataframe
body: <p>I'm trying to calculate the average amount for each user's transaction. What I mean is for each transaction, find the last 30 days transaction of that specific user and get the average amount of those transactions.</p>
  <p>Data: <a href="https://data.world/oklahoma/purchase-card-fiscal-year" rel="nofollow noreferrer">https://data.world/oklahoma/purchase-card-fiscal-year</a></p>
  <p>As a solution, which I don't find fancy, I try to use <code>apply</code> method such as</p>
  <pre><code>...
  import swifter
  
  transcations['Transaction_date_before_30day'] = pd.to_datetime(transcations['Transaction Date'], format='%m/%d/%Y %H:%M:%S %p') - timedelta(days=30)   
  transcations['Transaction_date_before_90day'] = pd.to_datetime(transcations['Transaction Date'], format='%m/%d/%Y %H:%M:%S %p') - timedelta(days=90)
  
  #transcations is the Pandas DataFrame for the csv
  
  def txn_amount_over_month(double_col):
        temp_df = transcations[(transcations.userId == double_col[1] ) &amp; (transcations['Transaction Date'] &gt;= double_col[0]) ]
        if len(temp_df) == 0:
          return 0
        return (temp_df.Amount.sum()) / len(temp_df)
  
  transcations['txn_amount_over_month'] = transcations[['Transaction_date_before_30day','userId']].swifter.apply(txn_amount_over_month, axis=1)
  </code></pre>
  <p>But it takes too long as expected, about 3-4 hours.</p>
  <p>I tried to use <code>rolling</code> method for pandas but I couldn't make it.</p>
  <p>Thanks in advance,</p>