04pallav
9/11/2017 - 10:29 PM

dataframe pivot

dataframe pivot

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())
####################### just created dframe nothing else


#First two value spassed are the ROW and COLUMN indexes, then finally an optional FILL VALUE
dframe_piv = dframe.pivot('date','variable','value')


http://nikgrozev.com/2015/07/01/reshaping-in-pandas-pivot-pivot-table-stack-and-unstack-explained-with-pictures/


###################
long to wide is pivot.
Pivot takes 3 arguments with the following names: index, columns(cat) , and values(num)

entries inside the column(cat) will be used to create new columns

index will have distinct values
values will go inside the table

p = d.pivot(index='Item', columns='CType')

If you omit values all numerical columns in the datframe will be used. multi index will be created

#################PIVOT TABLE 
table = pd.pivot_table(df, index=['item_id', 'hour', 'date'], columns='when', values='quantity')
multi indexes can be created using pivot tables