I'll use this Dataframe as example:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(3, 6),
columns=['a', 'b', 'c', '2010', '2011', '2012'])
which results in this data:
a b c 2010 2011 2012
0 -2.161845 -0.995818 -0.225338 0.107255 -1.114179 0.701679
1 1.083428 -1.473900 0.890769 -0.937312 0.781201 -0.043237
2 -1.187588 0.241896 0.465302 -0.194004 0.921763 -1.359859
Now I want to transpose (stack) columns '2010', '2011' and '2012' into rows to be able to get:
a b c
-2.161845 -0.995818 -0.225338 2010 0.107255
1.083428 -1.473900 0.890769 2010 -0.937312
-1.187588 0.241896 0.465302 2010 -0.194004
-2.161845 -0.995818 -0.225338 2011 -1.114179
1.083428 -1.473900 0.890769 2011 0.781201
-1.187588 0.241896 0.465302 2011 0.921763
-2.161845 -0.995818 -0.225338 2012 0.701679
1.083428 -1.473900 0.890769 2012 -0.043237
-1.187588 0.241896 0.465302 2012 -1.359859
By using df.stack() pandas "stacks" all columns into rows, while I want to stack just those pointed. So my qestion is how to transpose arbitrary columns to rows in pandas Dataframe?