In part 1 of the Data in Motorsport series, I introduced the amazing FastF1 Python package. In this blog, I’ll show you how you can pull some telemetry data with just a few lines of code.

Python is, of course, a prerequisite for creating and running this code, and all instructions for installing the FastF1 package can be found in its excellent documentation. Once that’s installed, you’re good to go!

My plan for this project is to use telemetry data in Tableau to make some interesting visuals. To get me started, I just need data for one lap of a Grand Prix. So, let’s get the data!

 

The Code

Firstly, the code below uses the FastF1 package to take the data from the recent Belgian Grand Prix at Spa-Francorchamps. It then finds race-winner Max Verstappen’s fastest lap from the race. Finally, it exports all available telemetry for that lap to a .csv file in the current working directory. It’s a simple as that!

# Import the required packages
import os
import pandas as pd
import fastf1
from fastf1.core import Laps
# Enable caching to avoid unnecessary strain on the API
# Replace "fastf1cache" with your cache location
fastf1.Cache.enable_cache('fastf1cache')
# Use variables year, wknd, ses, and driver to specify the race, session and driver
year = 2022
wknd = 14
ses = 'R'
driver = 'VER'
# Obtain and load session data
session = fastf1.get_session(year, wknd, ses)
session.load()
# Select fastest lap for specified driver
lap = session.laps.pick_driver(driver).pick_fastest()
# Write telemetry data for lap to tel variable
tel = lap.get_telemetry()
# Export telemetry to current working directory as .csv file
cwd = os.getcwd()
tel.to_csv(cwd + "\\ver_tel_spa.csv")

 

The variables in this code can be updated to get data for any race (via “year” and “wknd”), session (“ses”), or driver (“driver”) of your choosing. You can even grab telemetry data for multiple laps, as long as only one driver is selected.

One thing to note is that telemetry data obtained via the get_telemetry() method will contain some interpolated results. The result is generated by merging the data from the get_car_data() and get_pos_data() methods. This essentially means that some data in the table is interpolated and, whilst convenient, is not the most accurate result you could obtain.

For this project, this is acceptable, it’s just something to be aware of.

 

What Next?

In the next blog in this series, I’ll be taking this telemetry into Tableau to explore the data behind a race lap from World Champion Max Verstappen!

For the community, the possibilities with the FastF1 package are virtually endless, so the world (of F1) is your oyster! It may require a little time and patience if you’re new to writing code, but it’s a great opportunity to dive in and learn.

For anyone keen to give Python a go, head to python.org, it should have everything you need to get started. The wonderful FastF1 documentation can be found here.

Happy coding!

 

Other Blogs in This Series

Data in Motorsport: The Best Online Resources for Formula 1 Data

Data in Motorsport: Creating a Race Track Visualization in Tableau

Jonathan Carter
Author: Jonathan Carter