Challenge - 5 Problems
Structured Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing structured array fields
What is the output of this code snippet that uses a NumPy structured array?
NumPy
import numpy as np arr = np.array([(1, 2.5, 'A'), (2, 3.5, 'B')], dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'U1')]) print(arr['y'])
Attempts:
2 left
💡 Hint
Remember that accessing a field by name returns all values in that field.
✗ Incorrect
The structured array has fields 'x', 'y', and 'z'. Accessing arr['y'] returns the array of floats [2.5, 3.5].
❓ data_output
intermediate2:00remaining
DataFrame column selection output
Given this pandas DataFrame, what is the output of selecting the 'age' column?
NumPy
import pandas as pd df = pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [25, 30]}) print(df['age'])
Attempts:
2 left
💡 Hint
Selecting a column returns a Series with index and name.
✗ Incorrect
df['age'] returns a pandas Series with index 0 and 1, values 25 and 30, and name 'age'.
🧠 Conceptual
advanced2:00remaining
Difference in data mutability between structured arrays and DataFrames
Which statement correctly describes a key difference in mutability between NumPy structured arrays and pandas DataFrames?
Attempts:
2 left
💡 Hint
Think about how flexible the structure is after creation.
✗ Incorrect
NumPy structured arrays have fixed fields defined at creation and cannot add or remove columns. Pandas DataFrames can add or remove columns dynamically.
🔧 Debug
advanced2:00remaining
Identify the error in structured array field assignment
What error will this code produce and why?
NumPy
import numpy as np arr = np.array([(1, 2.5), (2, 3.5)], dtype=[('a', 'i4'), ('b', 'f4')]) arr['a'] = [10, 20, 30]
Attempts:
2 left
💡 Hint
Check the length of the array being assigned compared to the structured array size.
✗ Incorrect
The structured array has 2 elements, but the assignment tries to assign 3 values, causing a ValueError due to shape mismatch.
🚀 Application
expert3:00remaining
Choosing the best data structure for mixed data types with missing values
You have a dataset with numeric, string, and missing values. You want to perform data analysis and easily handle missing data. Which data structure is best and why?
Attempts:
2 left
💡 Hint
Consider ease of handling missing data and mixed types in analysis.
✗ Incorrect
Pandas DataFrames are designed to handle mixed data types and missing values efficiently with many built-in functions.