Challenge - 5 Problems
Data Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this dtype check?
Given the DataFrame below, what will be the output of
df.dtypes?Pandas
import pandas as pd data = {'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3], 'C': ['x', 'y', 'z']} df = pd.DataFrame(data) print(df.dtypes)
Attempts:
2 left
💡 Hint
Check the default integer and float types pandas uses on your system.
✗ Incorrect
Pandas uses 64-bit integers and floats by default on most systems. Strings are stored as 'object' dtype.
❓ data_output
intermediate2:00remaining
How many columns are numeric in this DataFrame?
Using the DataFrame below, how many columns will
df.select_dtypes(include=['number']).shape[1] return?Pandas
import pandas as pd import numpy as np data = {'X': [1, 2, 3], 'Y': [4.5, 5.5, 6.5], 'Z': ['a', 'b', 'c'], 'W': [True, False, True]} df = pd.DataFrame(data) print(df.select_dtypes(include=['number']).shape[1])
Attempts:
2 left
💡 Hint
Boolean columns are considered numeric by default in pandas.
✗ Incorrect
Columns 'X' (int), 'Y' (float), and 'W' (boolean) are numeric. Booleans are included as numeric in pandas.
🔧 Debug
advanced2:00remaining
Why does this dtype conversion raise an error?
Consider this code snippet. Why does
df['col'] = df['col'].astype(int) raise an error?Pandas
import pandas as pd data = {'col': ['1', '2', 'three', '4']} df = pd.DataFrame(data) df['col'] = df['col'].astype(int)
Attempts:
2 left
💡 Hint
Check the values in the column carefully for non-numeric strings.
✗ Incorrect
The string 'three' cannot be converted to int, so pandas raises a ValueError.
🧠 Conceptual
advanced2:00remaining
Which dtype is best for storing categorical data efficiently?
You have a column with repeated string values representing categories. Which dtype should you use to save memory and speed up operations?
Attempts:
2 left
💡 Hint
Think about pandas special dtype for repeated categories.
✗ Incorrect
The 'category' dtype stores repeated values efficiently and speeds up comparisons.
🚀 Application
expert3:00remaining
Identify the dtype of each column after this operation
After running the code below, what are the dtypes of columns 'A', 'B', and 'C'?
Pandas
import pandas as pd import numpy as np data = {'A': [1, 2, 3], 'B': [1.0, 2.0, 3.0], 'C': ['a', 'b', 'c']} df = pd.DataFrame(data) df['A'] = df['A'].astype('float32') df['B'] = df['B'].astype('int32') df['C'] = df['C'].astype('category') print(df.dtypes)
Attempts:
2 left
💡 Hint
Check the explicit astype conversions carefully.
✗ Incorrect
Column 'A' is converted to float32, 'B' to int32, and 'C' to category dtype.