I am working with a large dataset with multiple columns. It looks similar to this:
import pandas as pd
df = pd.DataFrame(
{'Country': ["NL", "NL", "NL", "BE", "BE", "BE"],
"Year": [2010, 2011, 2012, 2010, 2011, 2012],
"Monkeys": [1, 4, 5, 2, 5, 4],
"Giraffes": [40, 50, 60, 20, 30, 35],
"Pigs": [10, 15, 20, 30, 35, 40]
})
Which looks like this:
Country Year Monkeys Giraffes Pigs
0 NL 2010 1 40 10
1 NL 2011 4 50 15
2 NL 2012 5 60 20
3 BE 2010 2 20 30
4 BE 2011 5 30 35
5 BE 2012 4 35 40
I want it to look like this:
Animals Country 2010 2011 2012
0 Monkeys NL 1 4 5
1 Giraffes NL 40 50 60
2 Pigs NL 10 15 20
3 Monkeys BE 2 5 4
4 Giraffes BE 20 30 35
5 Pigs BE 30 35 40
Using pivot_table and pivot methods I have been unable to get this to work. Is there anyone who can help me solve this?