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]:
In [4]:
# 'date' as rows
# 'variable' as columns
# 'value' as the fill values
dframe_piv = dframe.pivot('date','variable','value')
dframe_piv
Out[4]:
In [ ]: