
- 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 - Manipulating Structured Arrays
Manipulating Structured Arrays in NumPy
Manipulating structured arrays in NumPy means modifying, rearranging, or working with the data in these arrays as per your requirement.
Structured arrays are special arrays where each element can have multiple fields (like name, age, height), and each field can have a different data type (like strings, integers, or floats).
In NumPy, you can manipulate structured arrays in several ways −
- Accessing and Modifying Fields
- Adding New Fields
- Deleting Fields
- Sorting Arrays
- Filtering Arrays
- Combining Arrays
- Reshaping Arrays
- Splitting Arrays
Accessing and Modifying Fields
You can access a specific field in a structured array by using the field name as a key. This is similar to how you access values in a dictionary. For example, if you have a structured array with fields like name, age, and height, you can access the age field to retrieve all the ages stored in the array.
Once you have accessed a field, you can also modify its values. For instance, if you want to update someone's age in the array, you can do so by directly assigning a new value to the corresponding element in the age field.
Example
In the following example, we are accessing and modifying the 'age' field in a structured array. Specifically, we update the age of the first element (Alice) from 30 to 31 and then retrieve the updated ages −
import numpy as np # Define the dtype with field names and data types dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')] # Create the structured array with some initial data data = [('Alice', 30, 5.6), ('Bob', 25, 5.8), ('Charlie', 35, 5.9)] structured_array = np.array(data, dtype=dtype) # Accessing the 'age' field ages = structured_array['age'] print("Ages before modification:", ages) # Modifying the 'age' field - let's update Alice's age to 31 structured_array['age'][0] = 31 # Accessing the 'age' field again to see the changes print("Ages after modification:", structured_array['age'])
Following is the output obtained −
Ages before modification: [30 25 35] Ages after modification: [31 25 35]
Adding New Fields to Structured Arrays
To add a new field to an existing structured array, you need to create a new array with the additional field and copy the existing data over.
This process might be necessary when your data structure evolves and requires additional information.
Example
In this example, we are expanding an existing structured array by adding a new field called 'Grade'. We copy the existing data into a new array with the additional field and then populate the new 'Grade' field with corresponding values −
import numpy as np # Existing structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 35)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Define a new dtype with an additional field 'Grade' new_dtype = [('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4'), ('Grade', 'f4')] # Create a new structured array with the new dtype students_with_grade = np.zeros(students.shape, dtype=new_dtype) # Copy the old data for field in students.dtype.names: students_with_grade[field] = students[field] # Add data to the new 'Grade' field students_with_grade['Grade'] = [85.5, 90.0, 88.0] print(students_with_grade)
This will produce the following result −
[(1, 'Alice', 25, 85.5) (2, 'Bob', 23, 90. ) (3, 'Charlie', 35, 88. )]
Deleting Fields from a Structured Array
To remove a field, you must create a new structured array with a modified dtype that excludes the unwanted field and then copy the data from the original array to the new one.
Example
In the example below, we are removing the 'Age' field from an existing structured array by creating a new array with a reduced dtype. We then copy the relevant fields from the original array into the new one −
import numpy as np # Original structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 35)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Define a new dtype without the 'Age' field reduced_dtype = [('ID', 'i4'), ('Name', 'U10')] # Create a new structured array with the reduced dtype students_without_age = np.zeros(students.shape, dtype=reduced_dtype) # Copy the relevant fields for field in students_without_age.dtype.names: students_without_age[field] = students[field] # Verify the result print(students_without_age)
Following is the output of the above code −
[(1, 'Alice') (2, 'Bob') (3, 'Charlie')]
Sorting Structured Arrays
Sorting structured arrays in NumPy involves ordering the elements (rows) of the array based on one or more fields (columns).
Structured arrays can have multiple fields of different data types (e.g., integers, floats, strings), and sorting allows you to organize your data in a meaningful way, such as arranging records by age, name, or any other attribute.
Example
In the following example, we are sorting a structured array by the 'Age' field using the np.sort() function with the "order" parameter. This rearranges the records in ascending order based on the 'Age' values −
import numpy as np # Original structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 35)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Sort by 'Age' sorted_students = np.sort(students, order='Age') print(sorted_students)
The output obtained is as shown below −
[(2, 'Bob', 23) (1, 'Alice', 25) (3, 'Charlie', 35)]
Filtering Data in Structured Arrays
Filtering data in structured arrays with NumPy involves selecting subsets of data that meet specific criteria.
To filter a structured array, you use boolean indexing. This involves creating a boolean mask (an array of True and False values) based on a condition applied to one or more fields. You then use this mask to index into the original array and extract the desired subset of records.
Example
In this example, we are using a boolean mask to filter a structured array by selecting only those records where the 'Age' field is greater than 25 −
import numpy as np # Original structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 30)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Create a boolean mask where Age > 25 mask = students['Age'] > 25 # Apply the mask to filter the array filtered_students = students[mask] print(filtered_students)
After executing the above code, we get the following output −
[(3, 'Charlie', 30)]
Combining Structured Arrays
Combining structured arrays in NumPy is used to combine arrays with the same dtype along a single axis (usually the rows).
In NumPy, the np.concatenate() function is used to join arrays along an existing axis. For structured arrays, this requires that all arrays share the same dtype.
Example
In the example below, we are combining two structured arrays with identical data types into one array using np.concatenate() function −
import numpy as np # Define two structured arrays with the same dtype students1 = np.array([(1, 'Alice', 25), (2, 'Bob', 23)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) students2 = np.array([(3, 'Charlie', 30), (4, 'David', 28)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Concatenate the arrays combined_students = np.concatenate((students1, students2)) print(combined_students)
The result produced is as follows −
[(1, 'Alice', 25) (2, 'Bob', 23) (3, 'Charlie', 30) (4, 'David', 28)]
Reshaping Structured Arrays
Reshaping structured arrays in NumPy involves changing the shape of an array while preserving its data structure. This means that the total number of elements (rows) remains the same before and after reshaping.
In NumPy, the np.reshape() function is used to change the shape of the structured array.
Example
In the following example, we are reshaping a 1-D structured array into a 2-D array using np.reshape() function −
import numpy as np # Define a 1-D structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 30)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Reshape the array from 1-D to 2-D reshaped_students = np.reshape(students, (3, 1)) print(reshaped_students)
This transforms the array from a single row of records into a column format, while preserving the structured data as shown in the output below −
[[(1, 'Alice', 25)] [(2, 'Bob', 23)][(3, 'Charlie', 30)]]
Splitting Structured Arrays
Splitting structured arrays in NumPy involves dividing a single structured array into multiple arrays based on certain criteria or sizes.
In NumPy, the np.split() function is used to split an array into multiple sub-arrays along a specified axis. For structured arrays, this function requires that the array be split along the axis where the elements can be evenly distributed.
Example
In this example, we are splitting a structured array into two equal parts using np.split() function −
import numpy as np # Define a structured array students = np.array([(1, 'Alice', 25), (2, 'Bob', 23), (3, 'Charlie', 30), (4, 'David', 28)], dtype=[('ID', 'i4'), ('Name', 'U10'), ('Age', 'i4')]) # Split the array into 2 equal parts split_students = np.split(students, 2) print(split_students[0]) print(split_students[1])
We get the output as shown below −
[(1, 'Alice', 25) (2, 'Bob', 23)] [(3, 'Charlie', 30) (4, 'David', 28)]