How to Analyze Formula 1 Data with Python: A Beginner’s Tutorial
You want to analyze Formula 1 data, but you really don’t know how to get started? Then this guide is made exactly for you.
This tutorial will get you started with everything need to go analyze Formula 1 data yourself. It’ll show you through the basics of setting up your Python environment and help you to set up the basis of your analysis. This tutorial will also provide you with resources, explanations and tips throughout, so stay tuned!
Setting everyting up
Assuming you already Python installed, we start by creating the directory in which we will start working. You can do this in any preferred location on your computer (personally, I’m on MacOS and I’ll put it in a Documents subfolder). Mine will be called formula1_python .
Virtual Environment
It’s good practice to always work in a virtual environment when working in Python. This makes sure that the entire Python environment you use for a specific project is isolated from other projects. This makes sure that no conflicts between scripts, libraries and projects arise. To create a virtual environment on MacOS (if you’re on Windows, read this), we open Terminal and navigate to our current folder.
cd ~/path/to/formula1_pythonWe then want to pip install virtual environments for Python, which we do as follows:
pip install virtualenvNow we are in the folder we want to, and we have installed the virtualenv package, we can actually create the virtual environment:
virtualenv venvThis creates a virtual environment that is called “venv”. If you look in the folder, you’ll find a folder that’s called “venv”. Only thing remaining is to activate the virtual environment, making sure that all we do from that moment onwards is isolated from the rest of the Python environments on your machine.
source venv/bin/activateNow, you’ll probably see something like (venv) at the beginning of your command line, meaning that you successfully activated the virtual environment!
Installing requirements
We obviously want to install fastf1. This library allows us to collect all the Formula 1 data we need. So, we do the following:
pip install fastf1In addition, Jupyter Notebooks are really convenient for playing around with data. You can run all the code line-by-line, and directly view and explore the output of your code. This makes doing analyses very convenient. We therefore run the following in the command line:
pip install notebookIf you need any other package, just run pip install [package-name] to install it into your virtual environment.
Creating a notebook
Now, we can get started with our actual analysis. First of all, let’s launch our Jupyter notebook:
jupyter-notebookYour the Notebook server should have been started and the file browser should have been launched in your browser. To create a new notebook, click “New” in the top-right corner and select “Python”.
Getting started with Fastf1
Everything has been set up, so let me now explain in detail how the fastf1 library works. Fastf1 also has its own documentation, but if you’re an absolute beginner, you still might end up confused.
Let’s begin with the absolute basics: importing the libraries we need. For now, it’s only the fastf1 library and pandas. You can create new cells by pressing “B” on your keyboard, and run cells by doing shift + enter.






