The function pandas.pivot_table can be used to create spreadsheet-style pivot tables.
It takes a number of arguments
Data: A DataFrame Object
Values: A column or a list of columns to aggregate
Index: A column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If An array was passed, it is being used as the same manner as column values.
Columns: A column, Grouper, array which has the same length as data, or list of them. Keys to group by on the Pivot table column. If An array was passed, it is being used as the same manner as column values.
Aggfunc: function to use as aggregation, defaulting to Numpy.mean
ImportNumPy as NPImportPandas as PDImportDATETIMEDF= PD. DataFrame ({'A': [' One',' One',' Both','three'] * 6, 'B': ['A','B','C'] * 8, 'C': ['Foo','Foo','Foo','Bar','Bar','Bar'] * 4, 'D': Np.random.randn (24), 'E': Np.random.randn (24), 'F': [Datetime.datetime (i, 1) forIinchRange (1, 13)] +[Datetime.datetime (, I, 15) forIinchRange (1, 13)]}) pd.pivot_table (DF, index=['A','B'], columns=['C'], values='D', aggfunc=np.sum) pd.pivot_table (DF, index=['C'], columns=['A','B'], values='D', aggfunc= 'sum') pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc=np.sum) pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc=[Np.sum]) pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc={'D': Len,'E': Np.sum}) Pd.pivot_table (DF, index=['A','B'], columns=['C'], values=['D','E'], aggfunc={'D': Len,'E': [Np.sum, Np.mean]}) Pd.pivot_table (DF, index=PD. Grouper (freq='M', key='F'), columns='C', values='D', Aggfunc=np.sum)# kinda like resample
Pandas pivot Table pivot_table