0
0
NumPydata~30 mins

Structured arrays vs DataFrames in NumPy - Hands-On Comparison

Choose your learning style9 modes available
Structured arrays vs DataFrames
📖 Scenario: You work in a small shop that tracks sales data. You want to organize the sales information to analyze it easily. You will use two common ways to store data in Python: NumPy structured arrays and Pandas DataFrames.This project will help you see how to create and use both data structures with the same data.
🎯 Goal: Create a NumPy structured array and a Pandas DataFrame with the same sales data. Then, select and print the sales records where the amount is greater than 50.
📋 What You'll Learn
Create a NumPy structured array with fields: 'product' (string), 'quantity' (integer), and 'price' (float).
Create a Pandas DataFrame with the same data.
Filter and select sales where 'quantity' is greater than 50 in both data structures.
Print the filtered results.
💡 Why This Matters
🌍 Real World
Organizing and filtering sales data helps businesses understand which products sell more and manage inventory better.
💼 Career
Data scientists and analysts often use NumPy and Pandas to clean and analyze data efficiently.
Progress0 / 4 steps
1
Create a NumPy structured array
Create a NumPy structured array called sales_array with these exact entries: ('apple', 30, 0.5), ('banana', 60, 0.3), ('orange', 80, 0.7). Use the data type with fields: 'product' as a string of length 10, 'quantity' as integer, and 'price' as float.
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
Create a Pandas DataFrame
Import pandas as pd. Create a DataFrame called sales_df with columns 'product', 'quantity', and 'price' using the same data as in sales_array.
NumPy
Need a hint?

Use pd.DataFrame and pass a dictionary with keys as column names and values as the fields from sales_array.

3
Filter sales with quantity greater than 50
Create a variable called filtered_array that selects rows from sales_array where quantity is greater than 50. Also create a variable called filtered_df that selects rows from sales_df where quantity is greater than 50.
NumPy
Need a hint?

Use boolean indexing with sales_array['quantity'] > 50 and sales_df['quantity'] > 50 to filter.

4
Print the filtered results
Print the variables filtered_array and filtered_df to show the sales records where quantity is greater than 50.
NumPy
Need a hint?

Use print(filtered_array) and print(filtered_df) to show the filtered data.