F1 API - Season Data

Python
Jupyter Notebooks
Code Snippets
Pandas
APIs
JSON
Author

Allison Day

Published

August 31, 2024

There are a few different F1 APIs available, but I found the one from Ergast to be the most comprehensive. The Ergast API provides a lot of data, including information on drivers, constructors, circuits, ect. In this post, I will show yo

u how to use the Ergast API to get data on the 2021 F1 season.

Getting Started

To get started, you will need to install the fastf1 library. You can do this by running the following command in your terminal:

pip install fastf1

Importing Libraries

First, we need to import the libraries we will be using. In this case, we will be using the fastf1 library to get the data from the Ergast API and the pandas library to work with the data.

import fastf1
from fastf1.ergast import Ergast

import pandas as pd

Getting the Data

There are a few different tables we need to pull from to get the data we want. The main tables we will be using are the race schedule, the sessions, and the drivers. We will start by getting the race schedule data.

Race Schedule

The first table we will pull from is the Race Schedule. This table contains information on all the races that took place during the 2021 season, including the date, name, and location of each race.

In order to collect this data, we need to use the Ergast API. To see the documentation, you can go here Ergast API.

First, we need to initialize Ergast to get the data from the API. We can do this by running the following code:

There are a lot of parameters you can pass to the API, but for now, we will just pass the year 2021 to get the data for that season.

ergast = Ergast()

races = ergast.get_race_schedule(2021)
races = pd.read_csv("data/races.csv")
races
season round raceUrl raceName raceDate raceTime circuitId circuitUrl circuitName lat long locality country fp1Date fp2Date fp3Date qualifyingDate sprintDate
0 2021 1 http://en.wikipedia.org/wiki/2021_Bahrain_Gran... Bahrain Grand Prix 2021-03-28 15:00:00+00:00 bahrain http://en.wikipedia.org/wiki/Bahrain_Internati... Bahrain International Circuit 26.0325 50.51060 Sakhir Bahrain 2021-03-26 2021-03-26 2021-03-27 2021-03-27 NaN
1 2021 2 http://en.wikipedia.org/wiki/2021_Emilia_Romag... Emilia Romagna Grand Prix 2021-04-18 13:00:00+00:00 imola http://en.wikipedia.org/wiki/Autodromo_Enzo_e_... Autodromo Enzo e Dino Ferrari 44.3439 11.71670 Imola Italy 2021-04-16 2021-04-16 2021-04-17 2021-04-17 NaN
2 2021 3 http://en.wikipedia.org/wiki/2021_Portuguese_G... Portuguese Grand Prix 2021-05-02 14:00:00+00:00 portimao http://en.wikipedia.org/wiki/Algarve_Internati... Autódromo Internacional do Algarve 37.2270 -8.62670 Portimão Portugal 2021-04-30 2021-04-30 2021-05-01 2021-05-01 NaN
3 2021 4 http://en.wikipedia.org/wiki/2021_Spanish_Gran... Spanish Grand Prix 2021-05-09 13:00:00+00:00 catalunya http://en.wikipedia.org/wiki/Circuit_de_Barcel... Circuit de Barcelona-Catalunya 41.5700 2.26111 Montmeló Spain 2021-05-07 2021-05-07 2021-05-08 2021-05-08 NaN
4 2021 5 http://en.wikipedia.org/wiki/2021_Monaco_Grand... Monaco Grand Prix 2021-05-23 13:00:00+00:00 monaco http://en.wikipedia.org/wiki/Circuit_de_Monaco Circuit de Monaco 43.7347 7.42056 Monte-Carlo Monaco 2021-05-21 2021-05-21 2021-05-22 2021-05-22 NaN
5 2021 6 http://en.wikipedia.org/wiki/2021_Azerbaijan_G... Azerbaijan Grand Prix 2021-06-06 12:00:00+00:00 baku http://en.wikipedia.org/wiki/Baku_City_Circuit Baku City Circuit 40.3725 49.85330 Baku Azerbaijan 2021-06-04 2021-06-04 2021-06-05 2021-06-05 NaN
6 2021 7 http://en.wikipedia.org/wiki/2021_French_Grand... French Grand Prix 2021-06-20 13:00:00+00:00 ricard http://en.wikipedia.org/wiki/Paul_Ricard_Circuit Circuit Paul Ricard 43.2506 5.79167 Le Castellet France 2021-06-18 2021-06-18 2021-06-19 2021-06-19 NaN
7 2021 8 http://en.wikipedia.org/wiki/2021_Styrian_Gran... Styrian Grand Prix 2021-06-27 13:00:00+00:00 red_bull_ring http://en.wikipedia.org/wiki/Red_Bull_Ring Red Bull Ring 47.2197 14.76470 Spielberg Austria 2021-06-25 2021-06-25 2021-06-26 2021-06-26 NaN
8 2021 9 http://en.wikipedia.org/wiki/2021_Austrian_Gra... Austrian Grand Prix 2021-07-04 13:00:00+00:00 red_bull_ring http://en.wikipedia.org/wiki/Red_Bull_Ring Red Bull Ring 47.2197 14.76470 Spielberg Austria 2021-07-02 2021-07-02 2021-07-03 2021-07-03 NaN
9 2021 10 http://en.wikipedia.org/wiki/2021_British_Gran... British Grand Prix 2021-07-18 14:00:00+00:00 silverstone http://en.wikipedia.org/wiki/Silverstone_Circuit Silverstone Circuit 52.0786 -1.01694 Silverstone UK 2021-07-16 2021-07-17 NaN 2021-07-16 2021-07-17
10 2021 11 http://en.wikipedia.org/wiki/2021_Hungarian_Gr... Hungarian Grand Prix 2021-08-01 13:00:00+00:00 hungaroring http://en.wikipedia.org/wiki/Hungaroring Hungaroring 47.5789 19.24860 Budapest Hungary 2021-07-30 2021-07-30 2021-07-31 2021-07-31 NaN
11 2021 12 http://en.wikipedia.org/wiki/2021_Belgian_Gran... Belgian Grand Prix 2021-08-29 13:00:00+00:00 spa http://en.wikipedia.org/wiki/Circuit_de_Spa-Fr... Circuit de Spa-Francorchamps 50.4372 5.97139 Spa Belgium 2021-08-27 2021-08-27 2021-08-28 2021-08-28 NaN
12 2021 13 http://en.wikipedia.org/wiki/2021_Dutch_Grand_... Dutch Grand Prix 2021-09-05 13:00:00+00:00 zandvoort http://en.wikipedia.org/wiki/Circuit_Zandvoort Circuit Park Zandvoort 52.3888 4.54092 Zandvoort Netherlands 2021-09-03 2021-09-03 2021-09-04 2021-09-04 NaN
13 2021 14 http://en.wikipedia.org/wiki/2021_Italian_Gran... Italian Grand Prix 2021-09-12 13:00:00+00:00 monza http://en.wikipedia.org/wiki/Autodromo_Naziona... Autodromo Nazionale di Monza 45.6156 9.28111 Monza Italy 2021-09-10 2021-09-11 NaN 2021-09-10 2021-09-11
14 2021 15 http://en.wikipedia.org/wiki/2021_Russian_Gran... Russian Grand Prix 2021-09-26 12:00:00+00:00 sochi http://en.wikipedia.org/wiki/Sochi_Autodrom Sochi Autodrom 43.4057 39.95780 Sochi Russia 2021-09-24 2021-09-24 2021-09-25 2021-09-25 NaN
15 2021 16 http://en.wikipedia.org/wiki/2021_Turkish_Gran... Turkish Grand Prix 2021-10-10 12:00:00+00:00 istanbul http://en.wikipedia.org/wiki/Istanbul_Park Istanbul Park 40.9517 29.40500 Istanbul Turkey 2021-10-08 2021-10-08 2021-10-09 2021-10-09 NaN
16 2021 17 http://en.wikipedia.org/wiki/2021_United_State... United States Grand Prix 2021-10-24 19:00:00+00:00 americas http://en.wikipedia.org/wiki/Circuit_of_the_Am... Circuit of the Americas 30.1328 -97.64110 Austin USA 2021-10-22 2021-10-22 2021-10-23 2021-10-23 NaN
17 2021 18 http://en.wikipedia.org/wiki/2021_Mexican_Gran... Mexico City Grand Prix 2021-11-07 19:00:00+00:00 rodriguez http://en.wikipedia.org/wiki/Aut%C3%B3dromo_He... Autódromo Hermanos Rodríguez 19.4042 -99.09070 Mexico City Mexico 2021-11-05 2021-11-05 2021-11-06 2021-11-06 NaN
18 2021 19 http://en.wikipedia.org/wiki/2021_S%C3%A3o_Pau... São Paulo Grand Prix 2021-11-14 17:00:00+00:00 interlagos http://en.wikipedia.org/wiki/Aut%C3%B3dromo_Jo... Autódromo José Carlos Pace -23.7036 -46.69970 São Paulo Brazil 2021-11-12 2021-11-13 NaN 2021-11-12 2021-11-13
19 2021 20 http://en.wikipedia.org/wiki/2021_Qatar_Grand_... Qatar Grand Prix 2021-11-21 14:00:00+00:00 losail http://en.wikipedia.org/wiki/Losail_Internatio... Losail International Circuit 25.4900 51.45420 Al Daayen Qatar 2021-11-19 2021-11-19 2021-11-20 2021-11-20 NaN
20 2021 21 http://en.wikipedia.org/wiki/2021_Saudi_Arabia... Saudi Arabian Grand Prix 2021-12-05 17:30:00+00:00 jeddah http://en.wikipedia.org/wiki/Jeddah_Street_Cir... Jeddah Corniche Circuit 21.6319 39.10440 Jeddah Saudi Arabia 2021-12-03 2021-12-03 2021-12-04 2021-12-04 NaN
21 2021 22 http://en.wikipedia.org/wiki/2021_Abu_Dhabi_Gr... Abu Dhabi Grand Prix 2021-12-12 13:00:00+00:00 yas_marina http://en.wikipedia.org/wiki/Yas_Marina_Circuit Yas Marina Circuit 24.4672 54.60310 Abu Dhabi UAE 2021-12-10 2021-12-10 2021-12-11 2021-12-11 NaN

Race Results

The next table we want to pull from is the sessions. This table contains information on all the sessions that took place during the 2021 season, including the session type, date, and location of each session.

We are going to use the fastf1 library to get the data from the API. Here is the documentation for the fastf1 library fastf1.

In order to get the session data, we need to pass the year 2021, the race name, and the session type to the API.

session = fastf1.get_session(2021, 'Bahrain', "R")
session.load()

session_results = session.results
session_results
session_results = pd.read_csv("data/session_results.csv")
session_results
DriverNumber BroadcastName Abbreviation DriverId TeamName TeamColor TeamId FirstName LastName FullName ... CountryCode Position ClassifiedPosition GridPosition Q1 Q2 Q3 Time Status Points
0 44 L HAMILTON HAM hamilton Mercedes 00D2BE mercedes Lewis Hamilton Lewis Hamilton ... NaN 1.0 1 2.0 NaN NaN NaN 0 days 01:32:03.897000 Finished 25.0
1 33 M VERSTAPPEN VER max_verstappen Red Bull Racing 0600EF red_bull Max Verstappen Max Verstappen ... NaN 2.0 2 1.0 NaN NaN NaN 0 days 00:00:00.745000 Finished 18.0
2 77 V BOTTAS BOT bottas Mercedes 00D2BE mercedes Valtteri Bottas Valtteri Bottas ... NaN 3.0 3 3.0 NaN NaN NaN 0 days 00:00:37.383000 Finished 16.0
3 4 L NORRIS NOR norris McLaren FF9800 mclaren Lando Norris Lando Norris ... NaN 4.0 4 7.0 NaN NaN NaN 0 days 00:00:46.466000 Finished 12.0
4 11 S PEREZ PER perez Red Bull Racing 0600EF red_bull Sergio Perez Sergio Perez ... NaN 5.0 5 0.0 NaN NaN NaN 0 days 00:00:52.047000 Finished 10.0
5 16 C LECLERC LEC leclerc Ferrari DC0004 ferrari Charles Leclerc Charles Leclerc ... NaN 6.0 6 4.0 NaN NaN NaN 0 days 00:00:59.090000 Finished 8.0
6 3 D RICCIARDO RIC ricciardo McLaren FF9800 mclaren Daniel Ricciardo Daniel Ricciardo ... NaN 7.0 7 6.0 NaN NaN NaN NaN Finished 6.0
7 55 C SAINZ SAI sainz Ferrari DC0004 ferrari Carlos Sainz Carlos Sainz ... NaN 8.0 8 8.0 NaN NaN NaN NaN Finished 4.0
8 22 Y TSUNODA TSU tsunoda AlphaTauri 2B4562 alphatauri Yuki Tsunoda Yuki Tsunoda ... NaN 9.0 9 13.0 NaN NaN NaN NaN Finished 2.0
9 18 L STROLL STR stroll Aston Martin 006F62 aston_martin Lance Stroll Lance Stroll ... NaN 10.0 10 10.0 NaN NaN NaN NaN Finished 1.0
10 7 K RAIKKONEN RAI raikkonen Alfa Romeo Racing 900000 alfa Kimi Räikkönen Kimi Räikkönen ... NaN 11.0 11 14.0 NaN NaN NaN NaN Finished 0.0
11 99 A GIOVINAZZI GIO giovinazzi Alfa Romeo Racing 900000 alfa Antonio Giovinazzi Antonio Giovinazzi ... NaN 12.0 12 12.0 NaN NaN NaN NaN +1 Lap 0.0
12 31 E OCON OCO ocon Alpine 0090FF alpine Esteban Ocon Esteban Ocon ... NaN 13.0 13 16.0 NaN NaN NaN NaN +1 Lap 0.0
13 63 G RUSSELL RUS russell Williams 005AFF williams George Russell George Russell ... NaN 14.0 14 15.0 NaN NaN NaN NaN +1 Lap 0.0
14 5 S VETTEL VET vettel Aston Martin 006F62 aston_martin Sebastian Vettel Sebastian Vettel ... NaN 15.0 15 20.0 NaN NaN NaN NaN +1 Lap 0.0
15 47 M SCHUMACHER MSC mick_schumacher Haas F1 Team FFFFFF haas Mick Schumacher Mick Schumacher ... NaN 16.0 16 18.0 NaN NaN NaN NaN +1 Lap 0.0
16 10 P GASLY GAS gasly AlphaTauri 2B4562 alphatauri Pierre Gasly Pierre Gasly ... NaN 17.0 17 5.0 NaN NaN NaN NaN Retired 0.0
17 6 N LATIFI LAT latifi Williams 005AFF williams Nicholas Latifi Nicholas Latifi ... NaN 18.0 18 17.0 NaN NaN NaN NaN Retired 0.0
18 14 F ALONSO ALO alonso Alpine 0090FF alpine Fernando Alonso Fernando Alonso ... NaN 19.0 R 9.0 NaN NaN NaN NaN Brakes 0.0
19 9 N MAZEPIN MAZ mazepin Haas F1 Team FFFFFF haas Nikita Mazepin Nikita Mazepin ... NaN 20.0 R 19.0 NaN NaN NaN NaN Accident 0.0

20 rows × 21 columns

You can see that the data is returned in a pandas DataFrame. This makes it easy to work with the data and analyze it further. You also have to do the same for the other sessions, like Qualifying “Q” and Practice “P”.

Drivers Results

This is actually a continuation of the previous section. We use the same session to get all the drivers’ laps data for that session.

drivers_results = session.laps.pick_drivers("VER")
drivers_results

You can actually get the data for all the drivers by passing the driver’s name to the pick_drivers method.

drivers_results = session.laps.pick_drivers(['VER', 'HAM'])
drivers_results
drivers_results = pd.read_csv("data/drivers_results.csv")
drivers_results
Time Driver DriverNumber LapTime LapNumber Stint PitOutTime PitInTime Sector1Time Sector2Time ... FreshTyre Team LapStartTime LapStartDate TrackStatus Position Deleted DeletedReason FastF1Generated IsAccurate
0 0 days 00:39:08.419000 VER 33 0 days 00:01:58.245000 1.0 1.0 NaN NaN NaN 0 days 00:00:53.703000 ... False Red Bull Racing 0 days 00:37:09.970000 2021-03-28 15:07:09.980 124 2.0 False NaN False False
1 0 days 00:41:30.825000 VER 33 0 days 00:02:22.406000 2.0 1.0 NaN NaN 0 days 00:00:45.874000 0 days 00:01:01.922000 ... False Red Bull Racing 0 days 00:39:08.419000 2021-03-28 15:09:08.429 4 1.0 False NaN False False
2 0 days 00:44:08.804000 VER 33 NaN 3.0 1.0 NaN NaN 0 days 00:00:44.111000 0 days 00:01:02.264000 ... False Red Bull Racing 0 days 00:41:30.825000 2021-03-28 15:11:30.835 41 1.0 False NaN False False
3 0 days 00:45:53.147000 VER 33 0 days 00:01:44.343000 4.0 1.0 NaN NaN 0 days 00:00:32.129000 0 days 00:00:41.273000 ... False Red Bull Racing 0 days 00:44:08.804000 2021-03-28 15:14:08.814 126 1.0 False NaN False False
4 0 days 00:47:37.776000 VER 33 0 days 00:01:44.629000 5.0 1.0 NaN NaN 0 days 00:00:39.328000 0 days 00:00:41.460000 ... False Red Bull Racing 0 days 00:45:53.147000 2021-03-28 15:15:53.157 671 1.0 False NaN False False
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
108 0 days 02:04:29.635000 HAM 44 0 days 00:01:36.155000 53.0 3.0 NaN NaN 0 days 00:00:30.810000 0 days 00:00:41.885000 ... True Mercedes 0 days 02:02:53.480000 2021-03-28 16:32:53.490 1 1.0 False NaN False True
109 0 days 02:06:04.449000 HAM 44 0 days 00:01:34.814000 54.0 3.0 NaN NaN 0 days 00:00:30.734000 0 days 00:00:40.788000 ... True Mercedes 0 days 02:04:29.635000 2021-03-28 16:34:29.645 1 1.0 False NaN False True
110 0 days 02:07:39.155000 HAM 44 0 days 00:01:34.706000 55.0 3.0 NaN NaN 0 days 00:00:30.605000 0 days 00:00:40.960000 ... True Mercedes 0 days 02:06:04.449000 2021-03-28 16:36:04.459 1 1.0 False NaN False True
111 0 days 02:09:14.073000 HAM 44 0 days 00:01:34.918000 56.0 3.0 NaN NaN 0 days 00:00:30.431000 0 days 00:00:40.946000 ... True Mercedes 0 days 02:07:39.155000 2021-03-28 16:37:39.165 1 1.0 False NaN False True
112 0 days 02:11:59.252000 HAM 44 NaN 57.0 3.0 NaN NaN NaN NaN ... True Mercedes 0 days 02:09:14.073000 NaN 1 NaN False NaN True False

113 rows × 31 columns

Putting it all together

Now that we have all the data we need, we can put it all together to get a complete view of the 2021 F1 season.

This code only gets the final results of each driver through the season, but you can get more data by changing the session type to “Q” or “P” and passing the driver’s name to the pick_drivers method.


results = []

races = ergast.get_race_schedule(2021)

for rnd, race in races["raceName"].items():
    
    session = fastf1.get_session(2021, race, "R")
    session.load()
    
    session_results = session.results
    session_results["round"] = rnd + 1
    session_results["race"] = race.removesuffix(" Grand Prix")
    session_results["sprint"] = "False"
    session_results["season"] = 2021
    results.append(session_results)
        

all_results = pd.concat(results)
all_results

NOTE: The code above will take a while to run because it is pulling a lot of data from the API. It took me about 17 minutes to run on my machine.

all_results = pd.read_csv("data/all_results.csv")
all_results
DriverNumber BroadcastName Abbreviation DriverId TeamName TeamColor TeamId FirstName LastName FullName ... Q1 Q2 Q3 Time Status Points round race sprint season
0 44 L HAMILTON HAM hamilton Mercedes 00D2BE mercedes Lewis Hamilton Lewis Hamilton ... NaN NaN NaN 0 days 01:32:03.897000 Finished 25.0 1 Bahrain False 2021
1 33 M VERSTAPPEN VER max_verstappen Red Bull Racing 0600EF red_bull Max Verstappen Max Verstappen ... NaN NaN NaN 0 days 00:00:00.745000 Finished 18.0 1 Bahrain False 2021
2 77 V BOTTAS BOT bottas Mercedes 00D2BE mercedes Valtteri Bottas Valtteri Bottas ... NaN NaN NaN 0 days 00:00:37.383000 Finished 16.0 1 Bahrain False 2021
3 4 L NORRIS NOR norris McLaren FF9800 mclaren Lando Norris Lando Norris ... NaN NaN NaN 0 days 00:00:46.466000 Finished 12.0 1 Bahrain False 2021
4 11 S PEREZ PER perez Red Bull Racing 0600EF red_bull Sergio Perez Sergio Perez ... NaN NaN NaN 0 days 00:00:52.047000 Finished 10.0 1 Bahrain False 2021
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
435 6 N LATIFI LAT latifi Williams 005AFF williams Nicholas Latifi Nicholas Latifi ... NaN NaN NaN NaN Accident 0.0 22 Abu Dhabi False 2021
436 99 A GIOVINAZZI GIO giovinazzi Alfa Romeo Racing 900000 alfa Antonio Giovinazzi Antonio Giovinazzi ... NaN NaN NaN NaN Gearbox 0.0 22 Abu Dhabi False 2021
437 63 G RUSSELL RUS russell Williams 005AFF williams George Russell George Russell ... NaN NaN NaN NaN Gearbox 0.0 22 Abu Dhabi False 2021
438 7 K RAIKKONEN RAI raikkonen Alfa Romeo Racing 900000 alfa Kimi Räikkönen Kimi Räikkönen ... NaN NaN NaN NaN Brakes 0.0 22 Abu Dhabi False 2021
439 9 NaN MAZ mazepin Haas F1 Team NaN haas Nikita Mazepin Nikita Mazepin ... NaN NaN NaN NaN Illness 0.0 22 Abu Dhabi False 2021

440 rows × 25 columns