DS105W – Data for Data Science
13 Feb 2025
16:03 – 16:15
(Early Insights)
🖥️ Live Demo: Some good practices I saw in your submissions.
dict.get('key', {})
instead of dict['key']
for a, b in zip(list_a, list_b)
import
ing the same library in the notebookCode We’ve Seen So Far
for
loopsWhere We’re Going
numpy
and pandas
)16:15 – 16:50
Key Benefits
Core Features
for
loops)Say you want to create a new list with the temperatures in Fahrenheit.
Python List
NumPy Types
np.int8
to np.int64
np.float32
, np.float64
np.bool_
, np.string_
💡 Choose types based on memory needs
🔗 Link: NumPy Data Types
# Single condition
hot = temps > 25
# This AND that
perfect = (temps >= 20) & (temps <= 25)
# This OR that
good = (temps >= 20) | (temps <= 25)
💡 Use &
and |
, not and
/or
⚠️ Using and
/or
with arrays will fail silently!
Use it like an if-else statement but for arrays.
Say temps = np.array([25, 28, 22, 27, 24])
:
You can also chain them together:
Beyond basic arithmetic, NumPy has many built-in functions.
We’ll work in the Jupyter notebook to:
16:50 – 17:15
+ Practice Exercise
I will give you two challenges to solve (in between the break).
numpy
💡 The exercises are in the Jupyter notebook I will share at the lecture.
17:15 – 18:00
Key Features
Advantages
.loc
and .iloc
Attributes.assign
MethodAdding New Columns
# Convert to Fahrenheit and add weather type
weather_df = (
weather_df
.assign(
temp_f=weather_df["temp"] * 9/5 + 32,
weather=np.where(weather_df["temp"] > 25, 'Hot', 'Cool')
)
)
💡 .assign()
is “chainable” and doesn’t modify the original DataFrame
⚠️ Avoid modifying DataFrames in-place:
.apply
Method (Series)Apply to Series
.apply
Method (DataFrame)Apply to DataFrame
def classify_weather(row):
if row['temp'] > 25 and row['rain'] < 1:
return 'Hot & Dry'
return 'Other'
# Apply to each row
weather_df.apply(classify_weather, axis=1)
You need to specify axis=1
if you want to apply the function to each row. axis=0
is the default and applies the function to each column.
💡 Pro tip: Chain pandas methods together
THE END
numpy
and pandas
)LSE DS105W (2024/25)