3

I have a dataframe that looks like this (dfFF):

    Sector  Country    Currency    Amount    Year
0   Charity    UK         GBP      328163    2018
1   Public     UK         GBP      120480    2017
2   Public     UK         GBP      123086    2017
3   Public     UK         GBP      125198    2017
4   Public     UK         GBP      1502793   2017
5   Public     UK         GBP      249999    2016

And I want to pivot it so I have Country as the rows and Sector on the columns with the Amount summed.

I use the code:

dfFFB = pd.pivot_table(dfFF, index=['Country'], columns=['Sector'], aggfunc='sum', fill_value=0)

And I get this:

    Year
Sector     Charity     Public
Country     
   UK       4036       28234

Which is summing the year, not the amount.

How would I force it to sum the Amount?

ScoutEU
  • 3,157
  • 13
  • 42
  • 78
  • 4
    Need define parameter `values` like `dfFFB = pd.pivot_table(dfFF, index='Country', columns='Sector', values='Amount', aggfunc='sum', fill_value=0)` – jezrael May 22 '18 at 12:59
  • add `values` argument to `pd.pivot_table(...)` – gyx-hh May 22 '18 at 12:59
  • Perfect, thank you. I was googling but was obviously asking the wrong question. Thank you! – ScoutEU May 22 '18 at 13:00

0 Answers0