0
0
Pandasdata~10 mins

Common dtype errors and fixes 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 integer type.

Pandas
df['age'] = df['age'].[1]()
Drag options to blanks, or click blank then click option'
Aconvert
Bto_numeric
Cparse
Dastype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to_numeric' without parentheses
Trying to use 'convert' which is not a pandas method
Using 'parse' which is unrelated
2fill in blank
medium

Complete the code to safely convert the 'price' column to numeric, forcing errors to NaN.

Pandas
df['price'] = pd.to_numeric(df['price'], errors=[1])
Drag options to blanks, or click blank then click option'
A'raise'
B'coerce'
C'ignore'
D'convert'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'raise' which throws an error on invalid data
Using 'ignore' which leaves invalid data unchanged
Using 'convert' which is not a valid option
3fill in blank
hard

Fix the error in this code that tries to convert a column to datetime.

Pandas
df['date'] = pd.to_datetime(df['date'], format=[1])
Drag options to blanks, or click blank then click option'
A'%m-%d-%Y'
B'%d-%m-%Y'
C'%Y-%m-%d'
D'%Y/%m/%d'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a format string that does not match the data
Forgetting to include separators in the format string
Using slashes instead of dashes when data uses dashes
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Pandas
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('a')
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of length
Using a wrong condition like startswith
Not filtering words by length
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 4.

Pandas
{ [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 4
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys
Not filtering by length correctly
Using word itself as value instead of length