Rotating Rows and columns pandas [duplicate]
Rotating Rows and columns pandas [duplicate]
This question already has an answer here:
Hi i have been trying to figure out how to rotate my rows and columns.I tried using the transpose but it didnt work. My dataframe looks like this
Country | Rates 2015 | Rates2016 | Rates2017 | GDP 2015| GDP 2016 | GDP2017
World | 6 | 7 | 8 | 2355 | 1235 | 324325
Isit possible to change to
| Rates | GDP
2015 | 6 | 2355
2016 | 7 | 1235
2017 | 8 | 34132
Yeah this is what im trying to do basically
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
df.T
first Explain your problem a little bit
– Muhammad Waleed
Sep 11 '18 at 10:30
1 Answer
1
Ensure that the transposed part is in the index. I can only assume that if you have tried df.T
that you have not set the index correctly
df.T
In [185]: df.set_index('Country Name').T
Out[185]:
Country Name A B C
2000 5 2 2
2005 3 1 2
2010 1 5 6
2015 7 2 9
If you want to name the index then you will also need to do
In [187]: df = df.set_index('Country Name').T
In [188]: df.index = df.index.rename('Year')
In [189]: df
Out[189]:
Country Name A B C
Year
2000 5 2 2
2005 3 1 2
2010 1 5 6
2015 7 2 9
Added as answer since the OP suggested they tried transposing unsuccessfully in an edit
– Alexander McFarlane
Sep 11 '18 at 10:36
Transpose
df.T
– Alexander McFarlane
Sep 11 '18 at 10:26