0
0
Pandasdata~10 mins

to_numeric() for safe conversion in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'age' column to numeric values safely.

Pandas
df['age'] = pd.to_numeric(df['age'], errors=[1])
Drag options to blanks, or click blank then click option'
A"coerce"
B"raise"
C"safe"
D"ignore"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ignore' does not convert invalid values.
Using 'raise' causes errors on invalid data.
2fill in blank
medium

Complete the code to convert a list of strings to numeric, forcing errors to NaN.

Pandas
numbers = pd.to_numeric(['10', '20', 'abc', '30'], errors=[1])
Drag options to blanks, or click blank then click option'
A"raise"
B"ignore"
C"coerce"
D"fail"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ignore' leaves invalid strings as is.
Using 'raise' throws an error.
3fill in blank
hard

Fix the error in the code to convert 'score' column to numeric, replacing errors with NaN.

Pandas
df['score'] = pd.to_numeric(df['score'], errors=[1])
Drag options to blanks, or click blank then click option'
A"coerce"
B"ignore"
C"raise"
D"fail"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'raise' causes exceptions on invalid data.
Using 'ignore' does not convert invalid values.
4fill in blank
hard

Fill both blanks to create a numeric conversion that replaces errors with NaN and downcasts to float.

Pandas
df['value'] = pd.to_numeric(df['value'], errors=[1], downcast=[2])
Drag options to blanks, or click blank then click option'
A"coerce"
B"ignore"
C"float"
D"integer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ignore' does not convert errors.
Using 'integer' downcast may fail if NaNs exist.
5fill in blank
hard

Fill all three blanks to convert a column safely, downcast to integer, and handle errors by coercion.

Pandas
df['count'] = pd.to_numeric(df['count'], errors=[1], downcast=[2]).fillna([3]).astype(int)
Drag options to blanks, or click blank then click option'
A"coerce"
B"ignore"
C"integer"
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Not filling NaNs before converting to int causes errors.
Using 'ignore' leaves invalid values unconverted.