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

Converting Types

If you need to go from a number back to a string, use the .astype() method:

df["customer_id"] = df["customer_id"].astype(str)

This is especially useful when you have a column with mixed types: the astype() call will convert any individual number rows into strings.

Also, remember that sometimes conversion isn't straightforward and needs to start with some basic cleanup. For example, say we need to remove a % sign and then convert from a string to a number... we can use str.replace() before using pd.to_numeric():

df["discount"] = df["discount"].str.replace("%", "", regex=False)
df["discount"] = pd.to_numeric(df["discount"], errors="coerce")

Assignment

SnackStack's accounting team has discount data in mixed percentage/decimal formats like "20.0%" and "0.15". The team needs to sort products by the biggest discounts but can't because of all the mixed types.

Complete the normalize_discounts function.