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

(260096, 10)
Loading...
Loading...
Channel Distributor 6098515.78 Online 3113650.20 Retail 8697066.51 Name: TotalPrice, dtype: float64
Loading...
Loading...

Olympic athletes and results

(271116, 15)
Loading...
Average weight of athletes: 70.70 kg
<Figure size 1200x400 with 1 Axes>
Sport Alpine Skiing 72.068110 Archery 70.011135 Art Competitions 75.290909 Athletics 69.249287 Badminton 68.171439 Baseball 85.707792 Basketball 85.777053 Beach Volleyball 79.089219 Biathlon 66.631419 Bobsleigh 89.250678 Boxing 65.249890 Canoeing 76.492615 Cross Country Skiing 65.877670 Curling 72.131707 Cycling 70.067944 Diving 60.572741 Equestrianism 67.803975 Fencing 71.387538 Figure Skating 59.543651 Football 70.446834 Freestyle Skiing 67.026835 Golf 71.194444 Gymnastics 56.916553 Handball 81.497151 Hockey 69.169909 Ice Hockey 80.810364 Judo 78.759867 Lacrosse 76.714286 Luge 77.264151 Modern Pentathlon 70.279540 Motorboating 77.000000 Nordic Combined 66.909560 Rhythmic Gymnastics 48.760976 Rowing 80.035863 Rugby 77.533333 Rugby Sevens 78.939799 Sailing 75.975154 Shooting 74.027877 Short Track Speed Skating 64.310484 Skeleton 74.166667 Ski Jumping 65.079014 Snowboarding 69.549189 Softball 67.471655 Speed Skating 70.026352 Swimming 70.588492 Synchronized Swimming 55.863529 Table Tennis 64.956449 Taekwondo 68.007475 Tennis 70.802291 Trampolining 59.322148 Triathlon 61.817490 Tug-Of-War 95.615385 Volleyball 78.900214 Water Polo 84.566446 Weightlifting 78.726663 Wrestling 75.495570 Name: Weight, dtype: float64
Loading...
Loading...
Loading...

Aggregate on multiple functions and/or values

Loading...
Loading...

Stack and unstack

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

Loading...
Sport Weight Aeronautics NaN Alpine Skiing 72.068110 Alpinism NaN Archery 70.011135 Art Competitions 75.290909 ... Height Tug-Of-War 182.480000 Volleyball 186.994822 Water Polo 184.834648 Weightlifting 167.824801 Wrestling 172.358586 Length: 132, dtype: float64

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