Table Fundamentals
1
Data 6 Summer 2024
LECTURE 08
Learning how to work with tables in Python.
Developed by students and faculty at UC Berkeley and Tuskegee University
Ice Breaker
Would you rather fight:
1 horse-sized duck
OR
100 duck-sized horses
2
Course Input
3
Week 2
Announcements!
4
Week 2
Announcements!
5
Today’s Roadmap
Lecture 08, Data 6 Summer 2024
6
Data 6 Content
7
Module 1
Introduction to Python and Jupyter Notebooks
Module 2
Data Visualization
Module 3
Working with Tables
Module 4
Simulation and Randomness
Exploratory Data Analysis
John Tukey (1915-2000) was a Princeton Mathematician & Statistician�and an Early Data Scientist. He coined the phrase Exploratory Data Analysis (EDA).
8
[Data Analysis & Statistics, Tukey 1965; Image from LIFE Magazine]
EDA is like detective work:
Exploratory data analysis is an attitude, a state of�flexibility, a willingness to look for those things that�we believe are not there, as well as those that�we believe to be there.
Today we will perform EDA by analyzing tables using Python.
Review: Table
Review: In this course, we often start with tabular data and perform EDA:
9
| Estimated total state population | Estimated high school graduate or higher (%) | Estimated bachelor's degree or higher (%) |
Alabama | 3,344,006 | 86.9 | 26.2 |
California | 26,665,143 | 83.9 | 34.7 |
Florida | 15,255,326 | 88.5 | 30.5 |
New York | 13,649,157 | 87.2 | 37.5 |
Texas | 18,449,851 | 84.4 | 30.7 |
row
column
Today’s goals
If you are new to programming, it is easy to get lost in the syntax of Python! For today, we will therefore focus on two meta-goals:
Review: In this course, we often start with tabular data and perform EDA:
10
| Estimated total state population | Estimated high school graduate or higher (%) | Estimated bachelor's degree or higher (%) |
Alabama | 3,344,006 | 86.9 | 26.2 |
California | 26,665,143 | 83.9 | 34.7 |
Florida | 15,255,326 | 88.5 | 30.5 |
New York | 13,649,157 | 87.2 | 37.5 |
Texas | 18,449,851 | 84.4 | 30.7 |
Table Attributes and Methods
11
1. Table Attributes and Methods
2. select, drop, and add columns
3. Create new tables
5. More Table Methods and Documentation
6. Demo: WNBA Data
➤
First, assign a Table to a name
To work with this table, we first load it in from a file called data/cal_unis.csv:
schools = Table.read_table('data/cal_unis.csv')
This lecture we will return to the dataset of public four-year colleges and universities in California (source):
12
assigns RHS value to schools name
returns a datascience Table from the given data file
Then we use schools to call Table methods and attributes.
Always assign your table to a name!
Next, use the documentation
We start with asking early questions about the table, such as # rows, # columns, and getting specific columns.
13
Access Su24 datascience documentation: here
Quick-check 1
14
Quick check
We start with asking early questions about the table, such as # rows, # columns, and getting specific columns.
Using the documentation, fill in the following code to:
15
Access Su24 datascience documentation: here
Attributes vs. Methods
Table attributes
Table methods
16
Very important Python datascience library paradigm
Quick-check 2
17
What is going to happen? .take, .sort
What will be the output of each cell? Example: A, B, C means the outputs are table A, then table B, then table C.
18
schools�(before cells are run)
What is going to happen? .take, .sort
What will be the output of each cell? Example: A, B, C means the outputs are table A, then table B, then table C.
19
E. Error
D.
A.
B.
C.
Select, drop, and add columns
20
➤
1. Table Attributes and Methods
2. select, drop, and add columns
3. Create new tables
5. More Table Methods and Documentation
6. Demo: WNBA Data
Column-first paradigm
Because columns represent variables, we will first learn a column-first approach to tables:
Now to the notebook for two demos!
21
Reorder Columns
How do we reorder the columns to get the following table?
Hint: use one of .select, .drop, or .with_columns.
22
Reorder Columns: Solution
How do we reorder the columns to get the following table?
23
Quick Check 3
Fill in the three blanks so that the correct table is returned.
24
Quick Check
Create new tables
25
➤
1. Table Attributes and Methods
2. select, drop, and add columns
3. Create new tables
4. More Table Methods and Documentation
5. Demo: WNBA Data
What does the following code do?
26
What does the following code do? Solution
27
With this newly created table,
Add some new columns.
Assign the new table with new columns to states.
What does the following code do? Solution
28
Add 3 columns that have these labels…
…and that have these values.
Good programming style
29
Use whitespace to your advantage!
As a human, why is the code above potentially easier to read than the code below?
More Table Methods and Documentation
30
➤
1. Table Attributes and Methods
2. select, drop, and add columns
3. Create new tables
4. More Table Methods and Documentation
5. Demo: WNBA Data
(1/3) Navigating documentation
Early programmers often find documentation the most challenging.
Suppose we have our schools table:
31
(1/3) Navigating documentation
Early programmers often find documentation the most challenging.
Suppose we have our schools table:
32
Python tuple datatype�(fixed, ordered list of values)
(2/3) Navigating documentation:�Filtering with where
Often times, we will want to filter the rows of a table based on a specific condition, i.e., get only the rows where some condition is true.
33
# 1
# 2
We will learn more complicated uses of .where later in the course (“Predicates”), but for now we filter only on exact matches.
(3/3) Navigating documentation:�Be careful with .show
The following cells exhibit a tricky, but potentially common bug.
34
(3/3) Navigating documentation:�Be careful with .show
The following cells exhibit a tricky, but potentially common bug.
35
.show is purely for display purposes. It does not return anything—i.e., it returns None.
The second cell therefore assigns schools to None!
(3/3) Navigating documentation:�Be careful with .show
The following cells exhibit a tricky, but potentially common bug.
36
.show is useful when you want to display intermediate output for debugging purposes.
Questions?
37
Even More Practice
(There are a few extra exercises in today’s lecture notebook for you to check out on your own time.)
38
Demo: WNBA Data
39
➤
1. Table Attributes and Methods
2. select, drop, and add columns
3. Create new tables
4. More Table Methods and Documentation
5. Demo: WNBA Data
WNBA Data
Let’s apply the methods we just learned about to a dataset of WNBA statistics from the 2020 season. The data was downloaded from here.
40
Questions?
41
Even More Practice
42
Quick Check
In Conclusion…
43
Week 2
Announcements!
44
Recap
Next Time
45