In [1]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame

In [2]:
# copied from jmportilla Udemy-notes notebook

#import pandas testing utility
import pandas.util.testing as tm; tm.N = 3

#Create a unpivoted function
def unpivot(frame):
    N, K = frame.shape
    
    data = {'value' : frame.values.ravel('F'),
            'variable' : np.asarray(frame.columns).repeat(N),
            'date' : np.tile(np.asarray(frame.index), K)}
    
    # Return the DataFrame
    return DataFrame(data, columns=['date', 'variable', 'value'])

#Set the DataFrame we'll be using
dframe = unpivot(tm.makeTimeDataFrame())

In [3]:
dframe


Out[3]:
date variable value
0 2000-01-03 A -0.301919
1 2000-01-04 A -0.313023
2 2000-01-05 A -1.078716
3 2000-01-03 B -0.130250
4 2000-01-04 B -0.328752
5 2000-01-05 B -0.274305
6 2000-01-03 C 0.576738
7 2000-01-04 C 1.410463
8 2000-01-05 C 0.382619
9 2000-01-03 D -1.166316
10 2000-01-04 D 0.436788
11 2000-01-05 D 0.920818

In [4]:
# 'date' as rows
# 'variable' as columns
# 'value' as the fill values
dframe_piv = dframe.pivot('date','variable','value')

dframe_piv


Out[4]:
variable A B C D
date
2000-01-03 -0.301919 -0.130250 0.576738 -1.166316
2000-01-04 -0.313023 -0.328752 1.410463 0.436788
2000-01-05 -1.078716 -0.274305 0.382619 0.920818

In [ ]: