Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practical uses of structured arrays
📖 Scenario: You work in a small company that tracks employee information. You want to store each employee's name, age, and salary in a way that keeps all data together and easy to access.
🎯 Goal: Create a structured array to hold employee data, filter employees by age, and display the filtered results.
📋 What You'll Learn
Create a structured numpy array with fields: 'name' (string), 'age' (integer), and 'salary' (float).
Create a variable to hold the age threshold for filtering employees.
Use boolean indexing to select employees older than the age threshold.
Print the filtered structured array.
💡 Why This Matters
🌍 Real World
Companies often store employee records with multiple data types like names, ages, and salaries. Structured arrays help keep this data organized and easy to analyze.
💼 Career
Data analysts and scientists use structured arrays to handle mixed data types efficiently, enabling quick filtering and analysis in real-world datasets.
Progress0 / 4 steps
1
Create a structured array with employee data
Create a numpy structured array called employees with these exact entries: ('Alice', 28, 70000.0), ('Bob', 35, 85000.0), ('Charlie', 22, 48000.0). Use the data types: 'U10' for name, int for age, and float for salary.
NumPy
Hint
Use np.array with a list of tuples and specify dtype as a list of tuples with field names and types.
2
Set the age threshold for filtering
Create a variable called age_limit and set it to 25 to use as the age threshold for filtering employees.
NumPy
Hint
Just assign the number 25 to a variable named age_limit.
3
Filter employees older than the age limit
Create a variable called older_employees that selects all entries from employees where the age field is greater than age_limit.
NumPy
Hint
Use boolean indexing with employees['age'] > age_limit inside the brackets.
4
Print the filtered employees
Write a print statement to display the older_employees structured array.
NumPy
Hint
Use print(older_employees) to show the filtered array.
Practice
(1/5)
1. What is the main advantage of using numpy structured arrays in data science?
easy
A. They allow storing different data types in one array with named fields.
B. They only store integers efficiently.
C. They automatically visualize data.
D. They replace all pandas functionality.
Solution
Step 1: Understand structured arrays
Structured arrays let you store mixed data types in one array with named fields, like columns in a table.
Step 2: Compare options
Only They allow storing different data types in one array with named fields. correctly describes this main advantage. Others are incorrect or unrelated.
Final Answer:
They allow storing different data types in one array with named fields. -> Option A
Quick Check:
Structured arrays = mixed types + named fields [OK]
Hint: Remember: structured arrays hold mixed types with names [OK]
Common Mistakes:
Thinking structured arrays only store one data type
Confusing structured arrays with visualization tools
A. ValueError because comparison with > is invalid on structured arrays.
B. No error; it correctly filters entries with age > 25.
C. TypeError because 'age' field is not accessible.
D. SyntaxError due to wrong indexing syntax.
Solution
Step 1: Check filtering syntax
Filtering structured arrays by a field with a condition like data['age'] > 25 is valid and returns a boolean mask.
Step 2: Confirm no errors
The code correctly filters rows where age is greater than 25, so no error occurs.
Final Answer:
No error; it correctly filters entries with age > 25. -> Option B
Quick Check:
Filtering with boolean mask on field works [OK]
Hint: Use boolean masks on fields to filter structured arrays [OK]
Common Mistakes:
Thinking structured arrays can't be filtered by fields
Confusing syntax for filtering
Assuming comparison operators don't work on fields
5. You have a structured array of employees with fields 'name' (string), 'age' (int), and 'salary' (float). You want to find the average salary of employees older than 30. Which code snippet correctly does this?
hard
A. avg_salary = data[data['salary'] > 30]['age'].mean()
B. avg_salary = np.mean(data['salary'] > 30)
C. avg_salary = data['salary'][data['age'] > 30].mean()
D. avg_salary = data['salary'].mean(data['age'] > 30)
Solution
Step 1: Filter employees older than 30
Use boolean mask data['age'] > 30 to select salaries of employees older than 30.
Step 2: Calculate mean salary of filtered data
Apply .mean() on the filtered salary array to get average salary.
Final Answer:
avg_salary = data['salary'][data['age'] > 30].mean() -> Option C
Quick Check:
Filter by age, then mean salary = avg_salary = data['salary'][data['age'] > 30].mean() [OK]
Hint: Filter first, then compute mean on selected field [OK]