-
Notifications
You must be signed in to change notification settings - Fork 23
3. Advanced SQLAlchemy Queries
1
2
3
4
5
6
Determining the Overall Percentage of Females
7 8
If you have two tables that already have an established relationship, you can automatically use that relationship by just adding the columns we want from each table to the select statement.
9
If you aren't selecting columns from both tables or the two tables don't have a defined relationship, you can still use the .join()
method on a table to join it with another table and get extra data related to our query. The join()
takes the table object you want to join in as the first argument and a condition that indicates how the tables are related to the second argument. Finally, you use the .select_from()
method on the select statement to wrap the join clause. For example, in the video, Jason executed the following code to join the census
table to the state_fact
table such that the state
column of the census
table corresponded to the name
column of the state_fact
table.
stmt = stmt.select_from(
census.join(
state_fact, census.columns.state ==
state_fact.columns.name)
10
11
12
13
14
15
16
Often, you'll have tables that contain hierarchical data, such as employees and managers who are also employees. For this reason, you may wish to join a table to itself on different columns. The .alias()
method, which creates a copy of a table, helps accomplish this task. Because it's the same table, you only need a where clause to specify the join condition.
17
18
19
Fantastic work so far! As Jason discussed in the video, sometimes you may have the need to work on a large ResultProxy, and you may not have the memory to load all the results at once. To work around that issue, you can get blocks of rows from the ResultProxy by using the .fetchmany()
method inside a loop. With .fetchmany()
, give it an argument of the number of records you want. When you reach an empty list, there are no more rows left to fetch, and you have processed all the results of the query. Then you need to use the .close()
method to close out the connection to the database.
20
21
1. Python Data Science Toolbox (Part 1)
- 1. Writing your own functions
- 2. Default arguments, variable-(args, kwargs)
- 3. Lambda functions and error-handling
2. Python Data Science Toolbox (Part 2)
3. Importing Data in Python (Part 1)
- 1. Introduction and flat files
- 2. Importing data from other file types
- 3. Working with relational database
4. Importing Data in Python (Part 2)
- 1. Importing data from the Internet
- 2. Interacting with APIs to import data from the web
- 3. Diving deep into the Twitter API
5. Cleaning Data in Python**
- 1. Exploring your data**
- 2. Tidying data for analysis
- 3. Combining data for analysis
- 4. Cleaning data for analysis
- 5. Case study
6. pandas Foundations
- 1. Data ingestion & inspection
- 2. Exploratory data analysis**
- 3. Time series in pandas
- 4. Case Study - Sunlight in Austin
7. Manipulating DataFrames with pandas
- 1. Extracting and transforming data
- 2. Advanced indexing**
- 3. Rearranging and reshaping data
- 4. Grouping data(unique)
- 5. Bringing it all together
8. Introduction to Databases