title: Percent Increase, groupby, over time (using Python)
body: <p>I have a dataset, df, where I am trying to calculate the percent increase of a particular group over a time period. Here is the dataset:</p>
  <pre><code>    date      size       type
  
     1/1/2020   1          a
     1/1/2020   1          a
     1/1/2020   3          a
     1/1/2020   1          b
     1/1/2020   2          b
     1/1/2020   0          b
     2/1/2020   5          a
     2/1/2020   6          a
     2/1/2020   3          a
     2/1/2020   20         b
     2/1/2020   21         b
     2/1/2020   30         b
  </code></pre>
  <p>Desired output</p>
  <pre><code>  date         increase   diff   type
  
    1/1/2020     200%       2      a
    1/1/2020     -40%      -2      a
    2/1/2020     -100      -1      b
    2/1/2020      50%      10      b
  
  
  
  Percent Increase/Change is  final-inital/initial * 100
  
  example for a , we start at 1 then end at 3, 
  which is a 200% increase in the month of January
  </code></pre>
  <p>This is what I am doing:</p>
  <pre><code>import pandas as pd
  import numpy as np
  
  df['date'] = pd.to_datetime(df['date'])
  df1=df.groupby(['type', pd.Grouper(key='date', freq='1D')])['size'].sum()
  df1['increase'] = df1['size'].pct_change().mul(100)  
  df1['diff'] = df1['size'].diff() 
  </code></pre>
  <p>However, my dates are not grouping correctly. Since I only have dates that are consecutive first of the month, I figure I can just use freq = '1D'</p>
  <pre><code>    date             
  
     1/1/2020            
     1/1/2020             
     1/1/2020             
     1/1/2020            
  </code></pre>
  <p>I suppose I would have to groupby the type as well as the date, but I am not sure and I am still researching this.</p>
  <p>Any suggestion is appreciated.</p>
  <p><strong>Updated</strong></p>
  <p>I have a dataset, df, where I wish to calculate the percent increase of the sum of a particular group over a time period. Here is the dataset:</p>
  <pre><code>     date     size       type
  
  1/1/2020     1          a
  1/1/2020     1          a
  1/1/2020     1          a
  1/1/2020     2          b
  1/1/2020     5          b
  1/1/2020     6          b
  2/1/2020     20         a
  2/1/2020     21         a
  2/1/2020     10         a
  2/1/2020     1          b
  2/1/2020     4          b     
  2/1/2020     5          b
  </code></pre>
  <p>Desired output</p>
  <pre><code>(grouping by type and date to find sum)
  
  date        size    type
  
  1/1/2020    3       a
  2/1/2020    51      a
  1/1/2020    13      b
  2/1/2020    10      b 
  </code></pre>
  <p>I believe this will do it for the first part:</p>
  <pre><code>    df.groupby(['date','type']).sum()
  </code></pre>
  <p>(finding the increase and diff by type)</p>
  <p>Final Desired Output:</p>
  <pre><code> date        type      increase    diff
   
   2/1/2020    a         1600%       48    
   2/1/2020    b        -23.07%      -3
  
  
  
  Percent Increase/Change is  final-inital/initial * 100
  </code></pre>