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.






Transpose df.T

– Alexander McFarlane
Sep 11 '18 at 10:26


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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)