Query pandas dataframes!!

Published: 30 November 2022
on channel: Data Science with Josh
3,183
103

In this Short, I demonstrate how to "query" dataframes with Python using the pandas .loc property.

Source code:

Libraries
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine

Creating SQL Alchemy engine object
engine = create_engine('postgresql://postgres:youtube@localhost:5432/eSports.val')

Initiailizing connection to database
conn = engine.connect()

SQL query as a string to pass into pd.read_sql function
sql = 'SELECT * FROM players'

creating a dataframe
df = pd.read_sql_query(sql,conn)

creating a dataframe where country equals 'us'
USA = df.loc[df['country'] == 'us']

updating the dataframe to encompass countries that contain 'us'
USA = df.loc[df['country'].str.contains('us')]


On this page of the site you can watch the video online Query pandas dataframes!! with a duration of hours minute second in good quality, which was uploaded by the user Data Science with Josh 30 November 2022, share the link with friends and acquaintances, this video has already been watched 3,183 times on youtube and it was liked by 103 viewers. Enjoy your viewing!