Paul's Python CoursePPC

Project 4 of 12, for Yahya

Census Pivot Tables

Pivot tables of the census for Yahya's wall chart: by parish, by year and by trade, with the Archive's trades index merged in.

About 70 minutes, in 16 steps.

Sign up free to open itLog in

Act 5 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • YahyaThe census is going on the wall chart, broken down by parish, by year and by trade.
  • YahyaThe Archive has a trades index as well, forty years of cards saying things like fisherman is sea and baker is trade.
  • PaulSort it into piles and then count each pile, same as you would with screws.
  • YahyaI do sort my screws by length, actually. Don't tell Caitlin.

census.csv is the census from the Census Explorer. trades.csv is the trades index: one card per line, with an occupation and its sector.

Write four functions. Each leaves the tables it's given as they were.

  • parish_table(census): how many people lived in each parish in each census year, as a DataFrame with parishes down the side, years across the top, and 0 where there was nobody.
  • year_summary(census): a DataFrame with one row per census year and four columns: people, households (how many different households), average_age, and per_household (people divided by households).
  • with_sectors(census, trades): the census with a sector column from the index. Everyone stays, once each, in the census's order. An occupation the index hasn't got has the sector unlisted.
  • sector_table(census, trades): how many people were in each sector in each census year, laid out like parish_table.
What it prints
People by parish
year           1921  1951  1981  2011
parish
Bankside         29    35    21    18
Church Street    35    31    18    25
Hill             14    18     9    12
Northgate        12    14    15    13

By census
      people  households  average_age  per_household
year
1921      90          23         27.1            3.9
1951      98          26         25.3            3.8
1981      63          26         39.5            2.4
2011      68          26         33.2            2.6

By sector
year      1921  1951  1981  2011
sector
at home     15    15     7     2
children    37    44    11    20
land         5     5     3     9
sea         17    16    16    11
service      4     9    15     9
trade       12     9    11    12
unlisted     0     0     0     5

What you learn

What's in it

  1. Sort into piles, then count each pile
  2. Five people, three parishes
  3. Several answers for each pile
  4. People and trades on the Hill
  5. A grid of answers
  6. Nobody in Bankside
  7. The census, in piles
  8. Lining two tables up
  9. The trades index
  10. One card, two rows
  11. One card, filed twice
  12. How Paul would do it
  13. Fix Riya's version
  14. Make the wall chart's tables
  15. Ship it