title: Sum of a numeric columns into specific ranges and counting its occurrences
body: <p>I am quite new to programming field of Python.
  I have a dataset which needs to be modified. I tried few methods for sum part but I dont get the exact results.</p>
  <p>Dataset : <a href="https://i.stack.imgur.com/2AZeQ.png" rel="nofollow noreferrer">My data table</a></p>
  <p>Requirements:</p>
  <ol>
  <li><p>To categorize the debit and credit values into the following ranges/bins :
  a) 2000-4000
  b) 5000-8000
  c) 9000-20000</p>
  </li>
  <li><p>The sum of debit should be for <strong>20 days</strong> period like
  if the transaction happened on 2020-01-01 then
  the sum of credit should be from 2020-01-01 to 2020-01-20</p>
  </li>
  <li><p>I also want the record of occurrences i.e
  the number of times the value from the bins lies in the category</p>
  </li>
  </ol>
  <p>Required Result  : <a href="https://i.stack.imgur.com/LpPFx.png" rel="nofollow noreferrer">Result</a>]<a href="https://i.stack.imgur.com/LpPFx.png" rel="nofollow noreferrer">2</a></p>
  <p>The code I tried for credit values:</p>
  <pre class="lang-py prettyprint-override"><code>    EndDate = BM['transaction_date']+ pd.to_timedelta(20, unit='D') 
      StartDate= BM['transaction_date']
      
      dfx=BM
      dfx['EndDate'] = EndDate
      dfx['StartDate'] = StartDate
      dfx['Debit'] = dfx.apply(lambda x: BM.loc[(df['transaction_date'] &gt;= x.StartDate) &amp; 
                                                  (BM['transaction_date'] 
                                                     &lt;=x.EndDate),'Debit'].sum(), axis=1)
  </code></pre>
  <p>Code1-<a href="https://i.stack.imgur.com/bJsox.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bJsox.png" alt="result3" /></a>
  Code2-<a href="https://i.stack.imgur.com/wodmM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wodmM.png" alt="result4" /></a></p>
  <p>error : <a href="https://i.stack.imgur.com/yxei9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yxei9.png" alt="Error" /></a></p>