Lecture 6: Pandas DataFrames
Autumn 2026
1
Sli.do
#cse163
Questions?
Icebreaker
Favorite outdoor activity?
CSE 163
CSE 163: Pandas DataFrames
Announcements
2
Questions?
CSE 163: Pandas DataFrames
Today’s Roadmap
3
CSE 163: Pandas DataFrames
Importing Modules
4
# Method 1: Import module
import module
module.function()
# Method 2: Import and rename module
import module as m
m.function()
# Method 3: Import specific function from module
from module import function
function()
CSE 163: Pandas DataFrames
What is pandas?
pandas is a widely-used Python library for working with tabular data.
5
Whereas last lecture we stored stores a list of row dictionaries, pandas stores a collection of columns.
CSE 163: Pandas DataFrames
Importing Data
We can easily import a CSV file as a pandas DataFrame with pd.read_csv �(pd is the standard alias for pandas)
6
df = pd.read_csv('filename.csv')
DataFrame object
Use df or df.head() at the end of a code cell to display the DataFrame
CSE 163: Pandas DataFrames
Accessing Columns
pandas enables the selecting and modifying of columns using the df[col_name] syntax.
7
Changing a Column
Selecting a Column
df['column1']
df['column1'] = vals
calls_df['Address']
calls_df['New Column'] = 1
Example
CSE 163: Pandas DataFrames
Filtering
A common data processing task is to filter the rows in a table to only those matching a certain condition.
8
condition = df[col_name] == 'target value'
df[condition]
condition is a Series of boolean values
select only the rows where the corresponding value in condition is True
CSE 163: Pandas DataFrames
Filtering with Masks
9
mask = df['magnitude'] > 5
df[mask]
# Same as: data[data['magnitude'] > 5]
| id | year | month | day | latitude | longitude | name | magnitude |
30 | us20006i18 | 2016 | 7 | 27 | -24.286000 | -67.864700 | Chile | 5.60 |
114 | us20006i35 | 2016 | 7 | 27 | 36.492200 | 140.756800 | Japan | 5.30 |
421 | us1000683b | 2016 | 7 | 28 | -16.824200 | -172.515800 | Tonga | 5.10 |
df[(df['magnitude'] > 5) & ~(df['day'] == 27)]
Can use multiple filters: & (and), | (or), ~ (not)
CSE 163: Pandas DataFrames
Accessing Data in Pandas
10
series[<indexer>]
df[<indexer>]
df.loc[<row indexer>, <column indexer>]
CSE 163: Pandas DataFrames