title: Python | Groupby | Pivot | Using Percentages
body: <p>I am currently working with ratio's, but have hit a snag.</p>
  <p>Below is a small sample of the data I'm working with [There are other platforms like Instagram, Messenger etc] to give the below more context:</p>
  <pre><code>Date        Reach   Impressions Clicks  Landing_Page    Platform
  30/05/2017  27447   27939       90      68              Facebook
  30/05/2017  24299   24318       80      44              Facebook
  30/05/2017  9897    10081       33      25              Facebook
  30/05/2017  11696   11721       33      21              Facebook
  30/05/2017  53      55          1                       Facebook
  31/05/2017  46632   68757       213     143             Facebook
  31/05/2017  67478   73401       650     424             Facebook
  31/05/2017  38831   47577       136     77              Facebook
  31/05/2017  46834   52449       135     77              Facebook
  31/05/2017  273     531         12      10              Facebook
  1/06/2017   48307   72141       221     150             Facebook
  1/06/2017   64122   79501       202     106             Facebook
  1/06/2017   66810   71033       843     575             Facebook
  1/06/2017   46225   50003       138     76              Facebook
  1/06/2017   496     1043        16      15              Facebook
  </code></pre>
  <p>I have then created a new column:</p>
  <p><code>df[&quot;Click_To_Landing_Ratio] = df[&quot;Landing_Page] / df[&quot;Clicks&quot;] * 100</code></p>
  <p>I have tried the following:</p>
  <pre><code>round(df.pivot_table(index=&quot;Date&quot;, columns=&quot;Platform&quot;, values=&quot;Click_To_Landing_Ratio&quot;), 3)
  
  round(df.groupby([&quot;Date&quot;, &quot;Platform&quot;], axis=0)[&quot;Click_To_Landing_Ratio&quot;].mean().unstack(), 3)
  </code></pre>
  <p>When I Pivot this in excel (and create a calculated column), I get</p>
  <pre><code>Row Labels  Facebook    Grand Total
  30/05/2017  66.67%      66.67%
  31/05/2017  63.79%      63.79%
  1/06/2017   64.93%      64.93%
  2/06/2017   63.98%      63.98%
  </code></pre>
  <p>When I Pivot or Groupby this in Pandas, I get:</p>
  <pre><code>Row Labels  Facebook    Grand Total
  30/05/2017  53.990%     53.990% 
  31/05/2017  65.871%     65.871% 
  1/06/2017   67.476%     67.476%
  2/06/2017   64.031%     64.031%
  </code></pre>
  <p>I am guessing Pandas is averaging the row values for that day where as excel would be creating a total and dividing it by the total entries (if that makes sense).</p>
  <p>In short, my questions are:</p>
  <ol>
  <li>If you have ratios (percentages) on each individual row, is there a way to replicate the same result in Excel, but in Pandas?</li>
  <li>Can you get totals using groupby similar to Pandas (margins=True)?</li>
  </ol>
  <p>Any help would be greatly appreciated.</p>