0
0
NumPydata~30 mins

Practical uses of structured arrays in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(older_employees) to show the filtered array.