37
SQL Query into Pandas DataFrame - Part 1
I'm using a database from sqlitetutorial.net and SQLite Browser to play with the queries. From the database, I will only use one table for this post, let's use only the
Albums
table here. Or if you know Google Colab then It's great alternative for Jupyter Notebook.
Let's assume the
albums.csv
file is in the same directory as your python or ipynb file, then we can load it into pandas dataframe like this:import pandas as pd
albums_df = pd.read_csv("albums.csv")
For Part 1, I will only cover
SELECT
, WHERE
, LIMIT
and ORDER BY
of SQL in DataFrame syntax.SELECT *
FROM albums
albums_df
SELECT Title
FROM albums
albums_df[['Title']]
SELECT Title, ArtistId
FROM albums
albums_df[['Title', 'ArtistId']]
SELECT *
FROM albums
WHERE Title = 'The Best Of Van Halen, Vol. I'
albums_df[albums_df['Title'] == 'The Best Of Van Halen, Vol. I']
SELECT *
FROM albums
WHERE ArtistId = 2 AND AlbumId = 3
albums_df[(albums_df['ArtistId'] == 2) & (albums_df['AlbumId'] == 3) ]
SELECT *
FROM albums
WHERE ArtistId IN (8, 9, 10)
albums_df[albums_df['ArtistId'].isin([8,9,10])]
In Between:
SELECT *
FROM albums
WHERE Title LIKE '%The%'
albums_df[albums_df['Title'].str.contains('The')]
Starts With:
SELECT *
FROM albums
WHERE Title LIKE 'The%'
albums_df[albums_df['Title'].str.contains('^The')]
Ends With:
SELECT *
FROM albums
WHERE Title LIKE '% Hits'
albums_df[albums_df['Title'].str.contains(' Hits$')]
SELECT *
FROM albums
LIMIT 10
albums_df[0:10]
or
albums_df.head(10)
Ascending:
SELECT *
FROM albums
ORDER BY Title ASC
albums_df.sort_values(['Title'], ascending=True)
Descending:
SELECT *
FROM albums
ORDER BY Title DESC
albums_df.sort_values(['Title'], ascending=False)
To the Part 2
37