CSE 163
Pandas
��Hunter Schafer
This Time
Last Time
2
Group Work Tips
Running out of time?
Have some time left over?
3
Importing
Importing lets you use the contents defined in another Python file
4
# Method 1: Import module
import module
module.function()
# Method 1: Import module
import module
module.function()
# Method 2: Import and rename module
import module as m
m.function()
# 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()
DataFrame
5
| 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 |
Columns
Index (row)
Series
6
0 | California |
1 | Burma |
2 | California |
df['name']
df['name'][1] # 'Burma'
Filtering
�
7
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)]
Location
How to access data in pandas
Series
DataFrame
Options for indexers:
Remember the end of a slice is inclusive unlike Python’s standard
8
df[<indexer>]
df.loc[<row indexer>, <column indexer>]
series[<indexer>]
Group Work:
Best Practices
When you first working with this group:
Tips:
9