Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Pivot tables

  • Pivoting of data is the process of aggregating data across two or more categorical variables.

  • Closely related are:

    • Group by, where the number of categorical variables is flexible.

    • Cross tabulation, which is basically pivoting, but with different defaults and parameters.

  • In Pandas, pivot tables are based on

    • values: what to aggregate

    • index and column: layout of result, and

    • aggfunc: how to aggregate.

Sales data from Power BI examples

Olympic athletes and results

Aggregate on multiple functions and/or values

Stack and unstack

  • These operations switch between the groupby-format and the pivot_table-format.

Exercise

  • Extract winter olympics.

  • Make yearly medal statistics for all countries using pivoting to count medals.

    • Remove all countries that have no winter olympic medals.

  • Extract the top 10 countries that have the most winter olympic medals in sum.

  • Use the noc_regions.csv to exchange the NOC codes with region names.

  • Plot the resulting 10 curves as proportions of medals per year.

    • Place the legend outside the plot