3. Creating simple selections

In this part of the tutorial we’ll learn how to create and count simple selections, involving one variable and a chosen value or set of values.

Making a simple selection

Selections can be built using Variable objects with the Python equality operator == to specify chosen values:

>>> sweden = bookings["Destination"] == "29"

Normally, the == operator would test for equality and return either True or False. However, in py-apteco it is a shortcut for creating a selection:

>>> type(sweden)
<class 'apteco.query.SelectorClause'>

You can then get a count of the selection by calling the count() method on it:

>>> sweden.count()
25207

Because the Destination variable is on the Bookings table, this is a count of bookings. We’ll learn later on how to control which table we count.

Tip

Make sure you use the different = symbols correctly: use the == comparison operator with the FastStats variable and chosen value(s) to create the selection, and the single = to assign the result of this to a Python variable.

Specifying multiple values

You can specify multiple values by using a list:

>>> france_or_germany_bookings = bookings["Destination"] == ["06", "07"]
>>> france_or_germany_bookings.count()
985734

In fact, you can use any Python iterable, except for a string (which will be treated as a single value). Here is an example using a generator expression:

>>> people = my_session.tables["People"]
>>> high_earners = people["Income"] == (f"{i:02}" for i in range(7, 12))
>>> high_earners.values
['07', '08', '09', '10', '11']
>>> high_earners.count()
7114

The format string f"{i:02}" left-pads the number i with 0s to a width of 2, which matches the format of the Income selector codes.

As shown in these examples, when specifying values for Selector variables you need to use selector codes given as strings (Python str type variables). In the next part, we’ll learn about the different values we can give for other FastStats variable types.