We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Type Normalization

Raw data is so often the wrong type. Numbers, dates, and booleans often arrive as strings: "100", "2024-01-15", "Yes".

Type normalization means converting values into the types that make the most sense. Say you want the average shipping cost from an order export, but the values are strings:

df["shipping_cost"] = ["7.25", "12.00", "4.50"]
df["shipping_cost"].mean()
# TypeError

Converting to Numeric

pd.to_numeric() converts strings to numbers:

# Convert a column to numeric
df["shipping_cost"] = pd.to_numeric(df["shipping_cost"], errors="coerce")

# Now you can do math
df["shipping_cost"].mean()

The errors="coerce" option turns unconvertible values into NaN instead of crashing your code.

Checking Types

Before you convert anything, check what you actually have:

# Show types of all columns
print(df.dtypes)

# Check a specific column
print(df["shipping_cost"].dtype)

Assignment

Complete the split_healthy_unhealthy_devices function. It accepts a DataFrame and a threshold value, then converts the values in the temperature column to numbers and returns two DataFrames: healthy devices below the threshold and unhealthy devices at or above the threshold.