2. Accessing tables and variables

In this part of the tutorial we’ll learn about accessing some of the core objects from your FastStats system — tables and variables.

Tables

The FastStats system tables are accessible via the tables attribute on the Session. This is a single object which provides both list-like and dict-like access to the Table objects representing the tables.

You can retrieve a specific table using its name:

>>> bookings = my_session.tables["Bookings"]

You can loop through the tables:

>>> for table in my_session.tables:
...     print(table.name)
...
Households
People
Bookings
Policies
WebVisits
Communications
Journey History
Content
Responses Attributed

You can use the built-in len() function to count them:

>>> len(my_session.tables)
9

The Table objects have various attributes for the table’s metadata:

>>> print(
...     f"There are {bookings.total_records:,}"
...     f" {bookings.plural.lower()}"
...     f" in the system."
... )
There are 2,130,081 bookings in the system.

See also

See the Tables reference guide for full details of the Table metadata attributes.

Variables

The FastStats system variables are similarly accessible through the variables attribute on the Session. You can retrieve a variable using its name or description:

>>> occupation = my_session.variables["peOccu"]
>>> cost = my_session.variables["Cost"]

Each Table also has a variables attribute which works in the same way, providing access to the variables on that table:

>>> occupation = people.variables["peOccu"]
>>> cost = bookings.variables["Cost"]

For convenience you can also just index into the Table object itself:

>>> occupation = people["peOccu"]
>>> cost = bookings["Cost"]

As with tables above, variables also supports counting using len(), and looping:

>>> len(my_session.variables)
94
>>> len(bookings.variables)
14
>>> for var in bookings.variables:
...     if var.type == "Numeric":
...         print(var.description)
...
Cost
Profit

The Variable objects have attributes with metadata for the variable:

>>> cost.type
<VariableType.NUMERIC: 'Numeric'>
>>> cost.currency_symbol
'£'
>>> occupation.type
<VariableType.SELECTOR: 'Selector'>
>>> occupation.num_codes
11

Here, cost is a Numeric variable representing an amount of British Pounds Sterling (£), and occupation is a Selector variable with 11 different selector codes.

Note

Some attributes are common to all variables, while others vary according to the variables type. For full details see the Variables reference guide.

At the moment we’ve only seen how to access our variables and their attributes, but in the next part we’ll learn how to use them to build selections.