Pandas for more complex querying
Here, you'll become more familiar with the pandas function read_sql_query() by using it to execute a more complex query: a SELECT statement followed by both a WHERE clause AND an ORDER BY clause.
You'll build a DataFrame that contains the rows of the Employee table for which the EmployeeId is greater than or equal to 6 and you'll order these entries by BirthDate.
Latihan ini adalah bagian dari kursus
Introduction to Importing Data in Python
Petunjuk latihan
- Using the function
create_engine(), create an engine for the SQLite databaseChinook.sqliteand assign it to the variableengine. - Use the
pandasfunctionread_sql_query()to assign to the variabledfthe DataFrame of results from the following query: select all records from theEmployeetable where theEmployeeIdis greater than or equal to6and ordered byBirthDate(make sure to useWHEREandORDER BYin this precise order).
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Import packages
from sqlalchemy import create_engine
import pandas as pd
# Create engine: engine
# Execute query and store records in DataFrame: df
# Print head of DataFrame
print(df.head())