Querying a table

How to retrieve a custom table from the

To retrieve a table from the Census API, you need three things:

Retrieving a table

With those selected, we can use the CensusAPI.query_table() method to get our table:

from census21api import CensusAPI

api = CensusAPI()

population_type = "HRP"
area_type = "ctry"
dimensions = ("sex", "hh_tenure_9a")

api.query_table(population_type, area_type, dimensions)
ctry sex hh_tenure_9a count population_type
0 E92000001 1 -8 0 HRP
1 E92000001 1 0 3276595 HRP
2 E92000001 1 1 2178228 HRP
3 E92000001 1 2 109635 HRP
4 E92000001 1 3 1064598 HRP
5 E92000001 1 4 1108579 HRP
6 E92000001 1 5 1785514 HRP
7 E92000001 1 6 210267 HRP
8 E92000001 1 7 17429 HRP
9 E92000001 2 -8 0 HRP
10 E92000001 2 0 4348098 HRP
11 E92000001 2 1 4566144 HRP
12 E92000001 2 2 126316 HRP
13 E92000001 2 3 880554 HRP
14 E92000001 2 4 951932 HRP
15 E92000001 2 5 2488175 HRP
16 E92000001 2 6 310933 HRP
17 E92000001 2 7 13088 HRP
18 W92000004 1 -8 0 HRP
19 W92000004 1 0 219732 HRP
20 W92000004 1 1 129053 HRP
21 W92000004 1 2 2020 HRP
22 W92000004 1 3 66360 HRP
23 W92000004 1 4 56478 HRP
24 W92000004 1 5 87615 HRP
25 W92000004 1 6 14552 HRP
26 W92000004 1 7 1247 HRP
27 W92000004 2 -8 0 HRP
28 W92000004 2 0 292357 HRP
29 W92000004 2 1 248774 HRP
30 W92000004 2 2 2263 HRP
31 W92000004 2 3 50277 HRP
32 W92000004 2 4 48978 HRP
33 W92000004 2 5 107229 HRP
34 W92000004 2 6 19245 HRP
35 W92000004 2 7 933 HRP

Retrieving labelled data

Sometimes the encoded data can be a bit opaque. If you would prefer to use the labels for the area type and dimensions, use the use_id argument:

api.query_table(population_type, area_type, dimensions, use_id=False)
ctry sex hh_tenure_9a count population_type
0 England Female Does not apply 0 HRP
1 England Female Owned: Owns outright 3276595 HRP
2 England Female Owned: Owns with a mortgage or loan 2178228 HRP
3 England Female Shared ownership: Shared ownership 109635 HRP
4 England Female Social rented: Rents from council or Local Aut... 1064598 HRP
5 England Female Social rented: Other social rented 1108579 HRP
6 England Female Private rented: Private landlord or letting ag... 1785514 HRP
7 England Female Private rented: Other private rented 210267 HRP
8 England Female Lives rent free 17429 HRP
9 England Male Does not apply 0 HRP
10 England Male Owned: Owns outright 4348098 HRP
11 England Male Owned: Owns with a mortgage or loan 4566144 HRP
12 England Male Shared ownership: Shared ownership 126316 HRP
13 England Male Social rented: Rents from council or Local Aut... 880554 HRP
14 England Male Social rented: Other social rented 951932 HRP
15 England Male Private rented: Private landlord or letting ag... 2488175 HRP
16 England Male Private rented: Other private rented 310933 HRP
17 England Male Lives rent free 13088 HRP
18 Wales Female Does not apply 0 HRP
19 Wales Female Owned: Owns outright 219732 HRP
20 Wales Female Owned: Owns with a mortgage or loan 129053 HRP
21 Wales Female Shared ownership: Shared ownership 2020 HRP
22 Wales Female Social rented: Rents from council or Local Aut... 66360 HRP
23 Wales Female Social rented: Other social rented 56478 HRP
24 Wales Female Private rented: Private landlord or letting ag... 87615 HRP
25 Wales Female Private rented: Other private rented 14552 HRP
26 Wales Female Lives rent free 1247 HRP
27 Wales Male Does not apply 0 HRP
28 Wales Male Owned: Owns outright 292357 HRP
29 Wales Male Owned: Owns with a mortgage or loan 248774 HRP
30 Wales Male Shared ownership: Shared ownership 2263 HRP
31 Wales Male Social rented: Rents from council or Local Aut... 50277 HRP
32 Wales Male Social rented: Other social rented 48978 HRP
33 Wales Male Private rented: Private landlord or letting ag... 107229 HRP
34 Wales Male Private rented: Other private rented 19245 HRP
35 Wales Male Lives rent free 933 HRP