title: Pandas converting absolute value to percentage of multiple groupby rows
body: <p>I have a long format df that has an amount column (absolute values) aggregated up to 3 different levels of date, country and group.</p>
  <pre><code>import pandas as pd
  df  = pd.DataFrame.from_dict([{ &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Serbia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 33948 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Thailand&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 15857 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 42855 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Ukraine&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 57306 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Poland&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 52898 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 32330 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 33791 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 45050 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 13865 }, { &quot;date&quot;: &quot;2022-03&quot;, &quot;country&quot;: &quot;Sweden&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 45039 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Colombia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 9363 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Bangladesh&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 47121 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 18855 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 49383 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Turkmenistan&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 61386 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Kenya&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 40434 }, { &quot;date&quot;: &quot;2022-03&quot;, &quot;country&quot;: &quot;Nicaragua&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 3801 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 39416 }, { &quot;date&quot;: &quot;2022-03&quot;, &quot;country&quot;: &quot;Brazil&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 13657 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Colombia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 23473 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 19742 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 45098 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 15158 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 18376 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Slovenia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 71213 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Czech Republic&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 32744 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Netherlands&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 42706 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 40277 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;United States&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 3070 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Germany&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 17039 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 8714 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Malta&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 44230 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 33626 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Greece&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 72860 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 59254 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Japan&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 18136 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Venezuela&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 14065 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 36930 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Honduras&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 768 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Vietnam&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 33652 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Ukraine&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 54050 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 50304 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Peru&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 27157 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Brazil&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 15869 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Sweden&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 32451 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Mozambique&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 29659 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Argentina&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 25282 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Mongolia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 63027 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Sudan&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 5006 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;United States&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 73414 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 34759 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Brazil&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 636 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Philippines&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 59227 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 28537 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 23460 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Philippines&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 62968 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Ukraine&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 63908 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Ukraine&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 38263 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Botswana&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 15918 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 31156 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;France&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 64077 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 18932 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 45279 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 7849 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 52640 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Peru&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 19369 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Greece&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 20489 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 30177 }, { &quot;date&quot;: &quot;2021-07&quot;, &quot;country&quot;: &quot;Portugal&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 69521 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Thailand&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 17341 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Peru&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 27012 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Afghanistan&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 34146 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 57619 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Portugal&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 37319 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Denmark&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 18370 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;United States&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 4690 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 35333 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Indonesia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 74285 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Mexico&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 11260 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Ukraine&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 44389 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;France&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 29432 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Ecuador&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 24529 }, { &quot;date&quot;: &quot;2021-08&quot;, &quot;country&quot;: &quot;Democratic Republic of the Congo&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 5211 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Georgia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 54164 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;France&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 9046 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Sweden&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 10326 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Madagascar&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 70109 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 25702 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Poland&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 46625 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;Czech Republic&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 23806 }, { &quot;date&quot;: &quot;2021-06&quot;, &quot;country&quot;: &quot;Poland&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 63310 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;Poland&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 56290 }, { &quot;date&quot;: &quot;2021-12&quot;, &quot;country&quot;: &quot;Russia&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 45846 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Sweden&quot;, &quot;group&quot;: 3, &quot;amount&quot;: 26358 }, { &quot;date&quot;: &quot;2021-09&quot;, &quot;country&quot;: &quot;Colombia&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 14682 }, { &quot;date&quot;: &quot;2021-11&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 65021 }, { &quot;date&quot;: &quot;2022-02&quot;, &quot;country&quot;: &quot;Peru&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 29406 }, { &quot;date&quot;: &quot;2022-01&quot;, &quot;country&quot;: &quot;China&quot;, &quot;group&quot;: 1, &quot;amount&quot;: 57333 }, { &quot;date&quot;: &quot;2021-05&quot;, &quot;country&quot;: &quot;Philippines&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 28340 }, { &quot;date&quot;: &quot;2021-10&quot;, &quot;country&quot;: &quot;Japan&quot;, &quot;group&quot;: 2, &quot;amount&quot;: 37300 }])
  </code></pre>
  <p>example df</p>
  <pre><code>    date    country  group  amount
  0   2022-02 Serbia   3      33948
  1   2021-05 Thailand 3      15857
  2   2021-05 Russia   2      42855
  3   2021-11 Ukraine  3      57306
  4   2021-06 Poland   2      52898
  ...
  </code></pre>
  <p>Date can be any <code>yyyy-mm</code>, Country can be any country, and group can be either <code>1</code>, <code>2</code>, <code>3</code>.</p>
  <p>What I would like to do is group by date and country, and then for each group, work out the relative percentage of the grouped date and country.</p>
  <p>e.g. take the original df above to get something like (example of one date and one country):</p>
  <pre><code>date    country  group  amount
  2022-02 Serbia   1      33948
                   2      34567
                   3      96787
  </code></pre>
  <p>and then convert the amount to a percentage:</p>
  <pre><code>date    country  group  amount_percentage
  2022-02 Serbia   1      20.5
                   2      20.9
                   3      58.6
  </code></pre>
  <p>before collapsing back to the original format for all date and countries:</p>
  <pre><code>    date    country  group  amount_percentage
  0   2022-02 Serbia   1      20.5
  1   2022-02 Serbia   2      20.9
  2   2022-02 Serbia   3      58.6
  ...
  </code></pre>
  <p>My current approach to tackling this is:</p>
  <pre><code>df.groupby(['date', 'country', 'group'])['amount'].sum().unstack()
  </code></pre>
  <p>which gives me grouped date and country columns, and the groups becoming column with relative amounts.</p>
  <pre><code>date    country  1     2     3
  2022-02 Serbia   33948 34567 96787  
          USA      23457 67589 23456  
  ...
  </code></pre>
  <p>However I am not sure how to convert these to percentages of the row total, and then convert the dataframe back to final format.
  How would you address this last part?</p>