After playing some aggregation and grouping in thelast part, now we will play harder with table joins.
The Playground Database
We will be using the same SQLite database, but now we are going to use some tables. So get all the required csv fileshere
Preparing the DataFrame
importpandasaspdalbums_df=pd.read_csv("albums.csv")artists_df=pd.read_csv("artists.csv")
Join Queries into Pandas DataFrame
INNER JOIN:
SQL
:
SELECT*FROMalbumsJOINartistsONalbums.ArtistId=artists.ArtistId
or
SELECT*FROMalbumsINNERJOINartistsONalbums.ArtistId=artists.ArtistId
Pandas
:
# For the exact same column name on both tablealbums_df.merge(artists_df,on='ArtistId')# Defining the join column of each tablesalbums_df.merge(artists_df,left_on='ArtistId',right_on='ArtistId')# To make sure we use the INNER onealbums_df.merge(artists_df,left_on='ArtistId',right_on='ArtistId',how='inner')
LEFT JOIN
SQL
:
SELECT*FROMalbumsLEFTJOINartistsONalbums.ArtistId=artists.ArtistId
Pandas
:
albums_df.merge(artists_df,on='ArtistId',how='left')
RIGHT JOIN
SQL
:
SELECT*FROMalbumsRIGHTJOINartistsONalbums.ArtistId=artists.ArtistId
Pandas
:
albums_df.merge(artists_df,on='ArtistId',how='right')
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse