0
0
NumPydata~15 mins

Creating structured arrays in NumPy - Try It Yourself

Choose your learning style9 modes available
Creating Structured Arrays with NumPy
📖 Scenario: You work in a small company that tracks employee information. You want to store each employee's name, age, and salary in a structured way so you can analyze the data easily.
🎯 Goal: Create a NumPy structured array to hold employee data with fields for name, age, and salary. Then display the array.
📋 What You'll Learn
Use NumPy to create a structured array
Define the data type with fields: 'name' as string, 'age' as integer, and 'salary' as float
Fill the array with given employee data
Print the structured array
💡 Why This Matters
🌍 Real World
Structured arrays help store complex data like employee records, sensor data, or survey results in an organized way.
💼 Career
Data scientists and analysts often use structured arrays to handle mixed data types efficiently for analysis and reporting.
Progress0 / 4 steps
1
Create the employee data list
Create a list called employee_data with these exact tuples: ("Alice", 30, 70000.0), ("Bob", 25, 48000.0), and ("Charlie", 35, 120000.0).
NumPy
Need a hint?

Use square brackets to create a list of tuples. Each tuple has name, age, and salary.

2
Define the structured array data type
Create a variable called dtype that defines a NumPy structured data type with fields: 'name' as 'U10' (string of max length 10), 'age' as int, and 'salary' as float.
NumPy
Need a hint?

Use a list of tuples to define the dtype. Each tuple has the field name and the data type.

3
Create the structured array
Create a NumPy structured array called employees using np.array() with employee_data and dtype.
NumPy
Need a hint?

Use np.array() with the data and the dtype to create the structured array.

4
Print the structured array
Print the variable employees to display the structured array.
NumPy
Need a hint?

Use print(employees) to show the structured array.