oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Pivot Tables #384

Open oldoc63 opened 2 years ago

oldoc63 commented 2 years ago

When we perform a groupby across multiple columns, we often want to change how our data is stored. For instance, recall the example where we are running a chain of stores and have data about the number of sales at different locations on different days. In order to test our hypothesis it would be more useful if the table was formatted differently. Reorganizing a table in this way is called pivoting. The new table is called a pivot table.

df.pivot(columns='ColumnToPivot',
             index='ColumnToBeRows',
             values='ColumnsToBeValues')
oldoc63 commented 2 years ago

In the previous shoefly example, a DataFrame with the total number of shoes of each shoe_type / shoe_color combination was created. The purchasing manager complains that this DataFrame is confusing. Make it easier for her to compare purchases of different shoe colors of the same shoe type by creating a pivot table. Save your results to the variable shoe_counts_pivot.