The pandas.pivot_table() function creates a spreadsheet-style pivot table as a dataframe. The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on the index and columns of the resulting dataframe.
The simplest pivot tables must have a dataframe and an index/list of the index. Let's take a look at how to do this:
- Let's make a pivot table of a new dataframe that consists of the body-style, drive-wheels, length, width, height, curb-weight, and price columns:
new_dataset1 = df.filter(["body-style","drive-wheels", "length","width","height","curb-weight","price"],axis=1)#simplest pivot table with dataframe df and index body-styletable = pd.pivot_table(new_dataset1, index =["body-style"]) table ...