0

I am trying to pivot a Dataframe in Pandas but I get DataError: No numeric types to aggregate. I have data that looks like :

Year,Country,medal,date
1896,Afghanistan,Gold,1/1/2012
1896,Afghanistan,Silver,1/1/2012
1896,Afghanistan,Bronze,2/3/2012
1896,Algeria,Gold,3/4/2012
1896,Algeria,Silver,4/3/2012
1896,Algeria,Bronze,5/4/2012

What I want is:

Year,Country,Gold,Silver,Bronze
1896,Afghanistan,1/1/2012,1/1/2012,2/3/2012
1896,Algeria,3/4/2012,4/3/2012,5/4/2012

I tried

medals = df.pivot_table('date', ['Year', 'Country',], 'medal').reset_index()

I get DataError: No numeric types to aggregate. Any help would be appreciated.

Sam
  • 1,206
  • 2
  • 12
  • 27

3 Answers3

2

You have to specify aggfunc in this case, because it tries to aggregates a numeric column:

df.pivot_table(index=['Year', 'Country'], 
               columns='medal', 
               values='date', 
               aggfunc=lambda x: x).reset_index()

medal  Year      Country    Bronze      Gold    Silver
0      1896  Afghanistan  2/3/2012  1/1/2012  1/1/2012
1      1896      Algeria  5/4/2012  3/4/2012  4/3/2012
Erfan
  • 40,971
  • 8
  • 66
  • 78
0

.pivot_table is good for aggregating data, but they need to be numerical. In your case, you should rather use .pivot as follows:

df.pivot(index='Country', columns='medal', values='date')
Hugolmn
  • 1,530
  • 1
  • 7
  • 20
0

To your code, just add "aggfunc=np.sum"

df.pivot_table('date', ['Year', 'Country'], 'medal',aggfunc=np.sum).reset_index()

Raghav
  • 53
  • 2