
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Record Arrays
Record Aarrays in NumPy
Record arrays are similar to structured arrays but come with an additional feature: they allow you to access fields as attributes using attribute-style (dot notation) and index-style (dictionary-style) access.
This can make code more readable and easier to write, especially when dealing with complex data structures.
Creating Record Arrays
We can create record arrays in multiple ways, they are −
- By converting an existing structured array
- Directly defining a record array
- Using the np.recarray constructor
Converting a Structured Array to a Record Array
One of the simplest ways to create a record array is by converting an existing structured array using the view method. This approach allows you to maintain the structure of your data while gaining the convenience of attribute access.
Example
In the following example, we are converting a structured array to a record array in NumPy −
import numpy as np # Create a structured array structured_array = np.array([('Alice', 25, 5.5), ('Bob', 30, 6.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('height', 'f4')]) # Convert the structured array to a record array record_array = structured_array.view(np.recarray) # Access fields as attributes print(record_array.name) print(record_array.age)
Following is the output obtained −
['Alice' 'Bob'] [25 30]
Creating a Record Array Directly
Another way to create a record array is to define it directly using the np.rec.array() function. This method allows you to create a record array from scratch, specifying both the data and the structure.
Example
In the example below, we are we are directly creating a record array in NumPy, which allows us to access the fields as attributes using dot notation −
import numpy as np # Directly create a record array record_array_direct = np.rec.array([('Charlie', 35, 5.8), ('David', 40, 6.2)], dtype=[('name', 'U10'), ('age', 'i4'), ('height', 'f4')]) # Access fields as attributes print(record_array_direct.name) print(record_array_direct.height)
This will produce the following result −
['Charlie' 'David'] [5.8 6.2]
Using np.recarray Constructor
You can also create a record array using the np.recarray constructor. This method is less common but provides additional flexibility for specific use cases.
Example
In this example, we are creating a record array using np.recarray constructor, assigning data to it, and then accessing the fields name and age as attributes −
import numpy as np # Define a record array using np.recarray record_array_custom = np.recarray((2,), dtype=[('name', 'U10'), ('age', 'i4'), ('height', 'f4')]) # Assign data to the record array record_array_custom[0] = ('Eve', 28, 5.7) record_array_custom[1] = ('Frank', 33, 6.1) # Access fields as attributes print(record_array_custom.name) print(record_array_custom.age)
Following is the output of the above code −
['Eve' 'Frank'] [28 33]
Accessing Fields of Record Arrays
Once you have created a record array, you can access its fields (i.e., the columns of the structured data) as attributes, which simplifies data manipulation and querying.
The most common way to access the fields of a record array is by using the dot (.) notation. Each field of the record array becomes an attribute that you can access directly, similar to how you would access an attribute of an object in Python.
Example: Accessing Fields Using Attribute Notation
In the following example, we are creating a record array with fields name, age, and height, and then accessing each field as an attribute of the record array −
import numpy as np # Create a record array record_array = np.rec.array([('Alice', 25, 5.5), ('Bob', 30, 6.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('height', 'f4')]) # Accessing the 'name' field print(record_array.name) # Accessing the 'age' field print(record_array.age) # Accessing the 'height' field print(record_array.height)
The output obtained is as shown below −
['Alice' 'Bob'] [25 30] [5.5 6. ]
Example: Accessing Multiple Fields Simultaneously
In addition to accessing individual fields, you can access multiple fields at once. However, unlike with structured arrays, record arrays do not directly support simultaneous field access using the attribute notation.
Instead, you need to revert to the traditional indexing method if you want to retrieve multiple fields at once as shown in the example below −
import numpy as np # Create a record array record_array = np.rec.array([('Alice', 25, 5.5), ('Bob', 30, 6.0)], dtype=[('name', 'U10'), ('age', 'i4'), ('height', 'f4')]) # Access multiple fields using indexing multiple_fields = record_array[['name', 'height']] print(multiple_fields)
After executing the above code, we get the following output −
[('Alice', 5.5) ('Bob', 6. )]
Modifying Fields of Record Arrays
Modifying fields in a NumPy record array allows you to update or change the data within specific columns (fields) of your structured dataset. Record arrays provide a way to access and modify fields using attribute-style access.
For instance, you can modify the values of individual fields in the record array by accessing the field through the dot (.) notation and then applying the desired operation or assignment.
To add new fields to a record array, you can use a combination of np.concatenat() function (or similar) to create a new record array with the additional fields.
Example: Updating Fields
In the example below, we are creating a record array with fields "name", "age", and "height". We then change the "age" and "height" for specific records −
import numpy as np # Step 1: Define the initial record array with fields 'name', 'age', and 'height' dtype = [('name', 'S20'), ('age', 'i1'), ('height', 'f4')] data = [('Alice', 30, 5.5), ('Bob', 25, 6.0), ('Charlie', 35, 5.8)] record_array = np.array(data, dtype=dtype) print("Original Record Array:") print(record_array) # Step 2: Update the fields for specific records # Update 'age' and 'height' for 'Bob' record_array[record_array['name'] == b'Bob']['age'] = 26 record_array[record_array['name'] == b'Bob']['height'] = 6.1 # Update 'age' for 'Charlie' record_array[record_array['name'] == b'Charlie']['age'] = 36 print("\nUpdated Record Array:") print(record_array)
The result produced is as follows −
Original Record Array: [(b'Alice', 30, 5.5) (b'Bob', 25, 6. ) (b'Charlie', 35, 5.8)] Updated Record Array: [(b'Alice', 30, 5.5) (b'Bob', 25, 6. ) (b'Charlie', 35, 5.8)]
Example: Adding New Fields
In this example, we first define a record array that includes only "name" and "age" fields. We then create a new record array with an additional "height" field −
import numpy as np # Step 1: Define the initial record array with fields 'name' and 'age' dtype = [('name', 'S20'), ('age', 'i1')] data = [('Alice', 30), ('Bob', 25), ('Charlie', 35)] record_array = np.array(data, dtype=dtype) print("Original Record Array:") print(record_array) # Step 2: Define a new dtype that includes the new field 'height' new_dtype = [('name', 'S20'), ('age', 'i1'), ('height', 'f4')] # Step 3: Create a new record array with the new dtype, initialized with zeros or default values new_record_array = np.zeros(record_array.shape, dtype=new_dtype) # Step 4: Copy existing data into the new record array for field in ['name', 'age']: new_record_array[field] = record_array[field] # Optionally, set default values for the new field 'height' new_record_array['height'] = 0.0 print("\nRecord Array with New Field:") print(new_record_array)
We get the output as shown below −
Original Record Array: [(b'Alice', 30) (b'Bob', 25) (b'Charlie', 35)] Record Array with New Field: [(b'Alice', 30, 0.) (b'Bob', 25, 0.) (b'Charlie', 35, 0.)]
Combining Record Arrays
Combining record arrays in NumPy refers to merging or concatenating multiple record arrays into a single record array. This process can be useful when you need to combine datasets or extend existing datasets with additional rows.
Following are the ways for combining record arrays −
- Concatenation: Combine record arrays along an existing axis (typically along rows).
- Stacking: Stack record arrays along a new axis, which can be useful for adding a new dimension.
Example: Concatenating Record Arrays
In the following example, we create two record arrays and then concatenate them along the rows to form a single combined record array −
import numpy as np # Define two record arrays with fields 'name', 'age', and 'height' dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')] data1 = [('Alice', 25, 5.5), ('Bob', 30, 6.0)] data2 = [('Charlie', 35, 5.8), ('David', 40, 5.9)] # Create record arrays record_array1 = np.array(data1, dtype=dtype).view(np.recarray) record_array2 = np.array(data2, dtype=dtype).view(np.recarray) # Concatenate the record arrays along the rows (axis 0) combined_record_array = np.concatenate((record_array1, record_array2)) print("Combined record array:") print(combined_record_array)
Following is the output obtained −
Combined record array: [('Alice', 25, 5.5) ('Bob', 30, 6. ) ('Charlie', 35, 5.8) ('David', 40, 5.9)]
Example: Stacking Record Arrays
In the example below, we create two record arrays and then stack them along a new axis to form a 3D record array −
import numpy as np # Define two record arrays with fields 'name', 'age', and 'height' dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')] data1 = [('Alice', 25, 5.5), ('Bob', 30, 6.0)] data2 = [('Charlie', 35, 5.8), ('David', 40, 5.9)] # Create record arrays record_array1 = np.array(data1, dtype=dtype).view(np.recarray) record_array2 = np.array(data2, dtype=dtype).view(np.recarray) # Stack the record arrays along a new axis stacked_record_array = np.stack((record_array1, record_array2), axis=0) print("Stacked record array:") print(stacked_record_array)
This will produce the following result −
Stacked record array: [[('Alice', 25, 5.5) ('Bob', 30, 6. )] [('Charlie', 35, 5.8) ('David', 40, 5.9)]]
Filtering Record Arrays
Filtering record arrays means selecting specific rows or elements from a record array based on a condition or set of conditions.
This process is used to extract subsets of data that meet certain criteria, which helps us to do more focused analysis and manipulation.
Example
In this example, we create a Boolean mask to filter records where the "age" is greater than "30" from a record array −
import numpy as np # Define a record array with fields 'name', 'age', and 'height' dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')] data = [('Alice', 25, 5.5), ('Bob', 30, 6.0), ('Charlie', 35, 5.8), ('David', 40, 5.9)] record_array = np.array(data, dtype=dtype).view(np.recarray) # Create a Boolean mask for ages greater than 30 mask = record_array.age > 30 # Apply the mask to filter the record array filtered_record_array = record_array[mask] print("Filtered record array (ages > 30):") print(filtered_record_array)
Following is the output of the above code −
Filtered record array (ages > 30): [('Charlie', 35, 5.8) ('David', 40, 5.9)]