0
0
NumPydata~20 mins

Why structured arrays matter in NumPy - See It in Action

Choose your learning style9 modes available
Why structured arrays matter
📖 Scenario: Imagine you work at a small library. You have a list of books, and each book has a title, an author, and a year it was published. You want to keep all this information together in one place so you can easily find and use it.
🎯 Goal: You will create a structured array using numpy to store information about books. Then, you will access and print the data to see why structured arrays are useful for keeping related data organized.
📋 What You'll Learn
Use numpy to create a structured array
Include fields: title (string), author (string), and year (integer)
Access and print the structured array data
💡 Why This Matters
🌍 Real World
Libraries, stores, and many businesses keep records with different types of information together, like names, dates, and numbers.
💼 Career
Data scientists and analysts often use structured arrays to organize and analyze complex data efficiently.
Progress0 / 4 steps
1
Create a structured array with book data
Import numpy as np. Create a structured array called books with three entries. Each entry should have these fields: title (string of max length 20), author (string of max length 20), and year (integer). Use these exact values: ('The Hobbit', 'Tolkien', 1937), ('1984', 'Orwell', 1949), and ('Python 101', 'Smith', 2020).
NumPy
Need a hint?

Use np.array with a dtype that defines the fields and their types.

2
Create a variable to hold the field names
Create a variable called fields that holds the list of field names in the books structured array. Use books.dtype.names to get the field names.
NumPy
Need a hint?

Use books.dtype.names to get the tuple of field names.

3
Access the 'author' field from the structured array
Use the variable books to get all the authors. Create a variable called authors that contains the author field from books.
NumPy
Need a hint?

Access a field in a structured array by using array['field_name'].

4
Print the structured array and the authors
Print the entire books structured array. Then print the authors variable.
NumPy
Need a hint?

Use two print statements: one for books and one for authors.