0
0
NumPydata~30 mins

Record arrays in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Record Arrays in NumPy
📖 Scenario: Imagine you work in a small library. You want to keep track of books with their title, author, and year published. You will use a special kind of array called a record array to store this information neatly.
🎯 Goal: Create a NumPy record array to store book information, then select and display books published after a certain year.
📋 What You'll Learn
Create a NumPy record array with fields: 'title' (string), 'author' (string), and 'year' (integer).
Create a variable to hold the year threshold for filtering books.
Use a condition to select books published after the threshold year.
Print the filtered record array showing only those books.
💡 Why This Matters
🌍 Real World
Record arrays help store mixed data types in one array, useful for datasets like books, employees, or products.
💼 Career
Understanding record arrays is important for data scientists and analysts who work with structured data in NumPy.
Progress0 / 4 steps
1
Create a NumPy record array with book data
Create a NumPy record array called books with these exact entries: ('The Hobbit', 'J.R.R. Tolkien', 1937), ('1984', 'George Orwell', 1949), and ('To Kill a Mockingbird', 'Harper Lee', 1960). Use the data types 'U20' for title and author, and int for year.
NumPy
Need a hint?

Use np.array with a list of tuples and specify the dtype as a list of tuples for field names and types.

2
Set a year threshold for filtering books
Create a variable called year_threshold and set it to 1940.
NumPy
Need a hint?

Just create a variable named year_threshold and assign the number 1940 to it.

3
Select books published after the threshold year
Create a variable called recent_books that selects from books only those entries where the year is greater than year_threshold.
NumPy
Need a hint?

Use boolean indexing with books['year'] > year_threshold to filter the record array.

4
Print the filtered record array
Write a print statement to display the recent_books record array.
NumPy
Need a hint?

Use print(recent_books) to show the filtered books.