CSE 163
Pandas & DataFrames
Arpan Kapoor�Summer 2026��
#2348882
Announcements
#2348882
Importing
# 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()
#2348882
DataFrame
| id | year | month | day | latitude | longitude | name | magnitude |
0 | nc72666881 | 2016 | 7 | 27 | 37.672333 | -121.619000 | California | 1.43 |
1 | us20006i0y | 2016 | 7 | 27 | 21.514600 | 94.572100 | Burma | 4.90 |
2 | nc72666891 | 2016 | 7 | 27 | 37.576500 | -118.859167 | California | 0.06 |
Index (row)
Columns
#2348882
Series
df['name']
0 | California |
1 | Burma |
2 | California |
df['name'][1] # 'Burma'
#2348882
Series Operations
0 | California :) |
1 | Burma :) |
2 | California :) |
df['name’] += “ :)”
df[‘magnitude’] *= 2
0 | 2.86 |
1 | 9.80 |
2 | 0.12 |
#2348882
Series Operations
0 | California |
1 | Burma |
2 | California |
df['name’] == “Burma”
#2348882
Series Operations
False |
True |
False |
0 | California |
1 | Burma |
2 | California |
df['name’] == “Burma”
#2348882
Series Operations
| id | year | month | day | latitude | longitude | name | magnitude |
0 | nc72666881 | 2016 | 7 | 27 | 37.672333 | -121.619000 | California | 1.43 |
1 | us20006i0y | 2016 | 7 | 27 | 21.514600 | 94.572100 | Burma | 4.90 |
2 | nc72666891 | 2016 | 7 | 27 | 37.576500 | -118.859167 | California | 0.06 |
df['name’] == “Burma”
#2348882
Filtering
Can use multiple filters: & (and), | (or), ~ (not)
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)]
#2348882
Location: Accessing Data in Pandas
The end of a slice is inclusive in Pandas, unlike in standard Python!
series[<indexer>]
df[<indexer>]
df.loc[<row indexer>, <column indexer>]
Lesson Recap
#2348882