0
0
NumPydata~5 mins

Creating structured arrays in NumPy - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What is a structured array in NumPy?
A structured array is a special type of NumPy array that allows you to store different data types in each element, similar to a table with columns of different types.
Click to reveal answer
beginner
How do you define the data type for a structured array in NumPy?
You define a structured array's data type using a list of tuples, where each tuple contains a field name and its data type, for example: [('name', 'U10'), ('age', 'i4')] means a string field 'name' and an integer field 'age'.
Click to reveal answer
beginner
Show how to create a structured array with fields 'name' (string) and 'age' (integer) with two entries.
Use numpy.array with dtype: np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', 'i4')])
Click to reveal answer
beginner
How do you access the 'age' field from a structured array named 'data'?
You access it by using data['age'], which returns an array of all ages in the structured array.
Click to reveal answer
beginner
Why are structured arrays useful in data science?
They let you store and manipulate tabular data with different types efficiently, similar to a spreadsheet or database table, but with NumPy's speed and functionality.
Click to reveal answer
What does the dtype [('name', 'U10'), ('age', 'i4')] specify in a structured array?
AA string field 'name' with max length 10 and an integer field 'age'
BTwo integer fields named 'name' and 'age'
CA float field 'name' and a string field 'age'
DAn array of 10 strings and 4 integers
How do you create a structured array with fields 'x' and 'y' both as floats?
Anp.array([(1, 2)], dtype=[('x', 'i4'), ('y', 'i4')])
Bnp.array([1.0, 2.0], dtype='float')
Cnp.array([(1.0, 2.0)], dtype=[('x', 'f8'), ('y', 'f8')])
Dnp.array([{'x':1.0, 'y':2.0}])
How can you access the 'name' field of a structured array called 'arr'?
Aarr['name']
Barr.name
Carr[0]['name']
Darr.get('name')
What type of data can a structured array hold?
AOnly integers
BMultiple data types in different fields
COnly floats
DOnly strings
Which of these is a benefit of using structured arrays?
AOnly works with numeric data
BFaster than plain Python lists for strings only
CAutomatically sorts data
DEfficient storage of tabular data with mixed types
Explain how to create a structured array with fields for 'city' (string) and 'population' (integer).
Think about how to specify the dtype and how to pass data as tuples.
You got /3 concepts.
    Describe how you would access and print all values of a specific field in a structured array.
    Remember how to get a column from a table.
    You got /3 concepts.