Python – Learn Pandas with SQL Examples – Football Analytics Example

When working with data, you will often move between SQL databases and Pandas DataFrames. SQL is excellent for storing and retrieving data, while Pandas is ideal for analysis inside Python.

In this article, we show how both can be used together, using a football (soccer) mini-league dataset. We build a small SQLite database in memory, read the data into Pandas, and then solve real analytics questions.

There are neither pythons or pandas in Bulgaria. Just software.

  • Setup – SQLite and Pandas

We start by importing the libraries and creating three tables – [teams, players, matches]  inside an SQLite in-memory database.

Now, we have three tables.

  • Loading SQL Data into Pandas

pd.read_sql  does the magic to load either a table or a custom query directly.

At this point, the SQL data is ready for analysis with Pandas.

  • SQL vs Pandas – Filtering Rows

Task: Find forwards (FW) with more than 1200 minutes on the field:

SQL:

Pandas:

As expected, both return the same subset, one written in SQL and the other in Pandas.

  • Aggregating Goals

Task: Total goals per team:

SQL:

Pandas:

Both results show which team has scored more goals overall.

  • Joining Tables

Task: Add the city of each team to the players table.

SQL:

Pandas:

  • Building a League Table

The fun part: calculating points (3 for a win, 1 for a draw) and goal difference. Only with SQL this time.

This produces a proper football league ranking – teams sorted by points and then goal difference:

  • Quick Pandas Tricks

    • Top scorers with nlargest:

    • Age bands with pd.cut:

  • YouTube Video

  • GitHub link

https://github.com/Vitosh/Python_personal/tree/master/YouTube/041_Python-Learn-Pandas-with-Football-Analytics

Tagged with: , , , , ,