Pandas > Stack & unstack columns

Name
Code
Output
dataframe = pandas.DataFrame(numpy.random.randint(0,100,size=(3, 2)), columns=['C1','C2'])
print ("dataframe")
print (dataframe)
dataframe2 = dataframe.stack()
print ("stacked dataframe")
print (dataframe2)
dataframe3 = dataframe2.unstack()
print ("unstacked dataframe")
print (dataframe3)
dataframe
   C1  C2
0  51  54
1  44  72
2  66  35
stacked dataframe
0  C1    51
   C2    54
1  C1    44
   C2    72
2  C1    66
   C2    35
dtype: int64
unstacked dataframe
   C1  C2
0  51  54
1  44  72
2  66  35