Challenge - 5 Problems
Data Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code after changing data types?
Given the DataFrame below, what will be the data type of column 'A' after applying
astype('float')?Data Analysis Python
import pandas as pd df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]}) df['A'] = df['A'].astype('float') print(df['A'].dtype)
Attempts:
2 left
💡 Hint
Think about what
astype('float') does to string numbers.✗ Incorrect
The
astype('float') converts the string numbers in column 'A' to floating point numbers, so the dtype becomes float64.❓ data_output
intermediate2:00remaining
What is the resulting DataFrame after type conversion?
What will be the output DataFrame after converting column 'C' to integers?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'C': [1.1, 2.5, 3.9]}) df['C'] = df['C'].astype('int') print(df)
Attempts:
2 left
💡 Hint
Remember that converting float to int truncates the decimal part.
✗ Incorrect
When converting floats to integers using
astype('int'), the decimal part is dropped (not rounded). So 1.1 becomes 1, 2.5 becomes 2, and 3.9 becomes 3.🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This code tries to convert a column with mixed types to integer. What error does it raise?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'D': ['10', 'twenty', '30']}) df['D'] = df['D'].astype('int')
Attempts:
2 left
💡 Hint
Look at the string 'twenty' and think about converting it to int.
✗ Incorrect
The string 'twenty' cannot be converted to an integer, so Python raises a ValueError indicating the invalid literal.
🧠 Conceptual
advanced2:00remaining
Which data type conversion is NOT possible with astype()?
Which of the following conversions using
astype() will raise an error?Attempts:
2 left
💡 Hint
Think about converting text that is not a number to a numeric type.
✗ Incorrect
Converting a string column that contains non-numeric text (like words) to float will raise a ValueError because the text cannot be parsed as a number.
🚀 Application
expert3:00remaining
How many unique data types are in this DataFrame after conversions?
After running the code below, how many unique data types are present in the DataFrame?
Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'X': ['1', '2', '3'], 'Y': [1.0, 2.0, 3.0], 'Z': [True, False, True] }) df['X'] = df['X'].astype('int') df['Y'] = df['Y'].astype('int') df['Z'] = df['Z'].astype('int') print(df.dtypes.nunique())
Attempts:
2 left
💡 Hint
Check the data types after conversion carefully.
✗ Incorrect
After converting all columns to int, all columns have dtype int64, so there is only 1 unique data type.