Store Different Datatypes In One Numpy Array
Last Updated :
10 Feb, 2024
Storing diverse data types in a single NumPy array presents an effective approach to handling varied datasets efficiently. Although NumPy arrays are commonly homogeneous, situations may arise where managing multiple data types within a single array becomes necessary.
In this article, we will understand How To Store Different Datatypes In One Numpy Array
Store Different datatypes in one NumPy array
Let's see the simplest solution to store different datatypes in one NumPy array uses. NumPy to create three arrays with different data types: array1
with int32, array2
with float64, and array3
with object type using dtype. Then, the np.hstack
function horizontally stacks these arrays, creating combined_array
. The dtype parameter is used to explicitly set data types.
Python3
import numpy as np
# Create three arrays with different data types
array1 = np.array([1, 2, 3], dtype=np.int32)
array2 = np.array([1.5, 2.5, 3.5], dtype=np.float64)
array3 = np.array(['a', 'b', 'c'], dtype=object)
# Combine arrays horizontally using np.hstack
combined_array = np.hstack((array1, array2, array3))
print("Array 1:")
print(array1)
print("Array 2:")
print(array2)
print("Array 3:")
print(array3)
print("Combined Array:")
print(combined_array)
Output:
Array 1:
[1 2 3]
Array 2:
[1.5 2.5 3.5]
Array 3:
['a' 'b' 'c']
Combined Array:
[1 2 3 1.5 2.5 3.5 'a' 'b' 'c']
Record Arrays with dtype
In NumPy, you can use record arrays to store different data types within a single array. Record arrays allow you to define fields with different names and data types. Here's an example:
Python3
import numpy as np
dtype = np.dtype([('integer_field', np.int32), ('float_field', np.float64), ('string_field', 'U10')])
# Create a record array with different data types
record_array = np.array([(1, 1.5, 'one'), (2, 2.5, 'two'), (3, 3.5, 'three')], dtype=dtype)
print(record_array)
print(record_array.dtype)
Output:
[(1, 1.5, 'one') (2, 2.5, 'two') (3, 3.5, 'three')]
[('integer_field', '<i4'), ('float_field', '<f8'), ('string_field', '<U10')]
Structured Arrays with dtype
Structured arrays in NumPy allow you to create arrays with compound data types, where each element has multiple fields with different data types. The dtype
parameter is used to specify the data type for each field. Structured arrays are useful for representing structured data, such as records in a database Let's see the implementation with below code:
dtype
specifies the structured array's data type. The field 'keys' has a string data type ('U1'
), and the field 'data' has a 64-bit integer data type ('<i8'
).- The structured array is created with the specified data type.
Python3
import numpy as np
# Create a structured array with specified dtype
structured_array = np.array([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)],
dtype=[('keys', 'U1'), ('data', '<i8')])
print("Structured Array:")
print(structured_array)
print("\nKeys Field:")
print(structured_array['keys'])
print("\nData Field:")
print(structured_array['data'])
Output:
Structured Array:
[('a', 0) ('b', 1) ('c', 2) ('d', 3) ('e', 4)]
Keys Field:
['a' 'b' 'c' 'd' 'e']
Data Field:
[0 1 2 3 4]
Conclusion
The capacity to hold many data formats within a solitary NumPy array creates novel opportunities for effectively handling heterogeneous datasets.
Similar Reads
Different Ways to Create Numpy Arrays in Python Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create
3 min read
Import Text Files Into Numpy Arrays - Python We have to import data from text files into Numpy arrays in Python. By using the numpy.loadtxt() and numpy.genfromtxt() functions, we can efficiently read data from text files and store it as arrays for further processing.numpy.loadtxt( ) - Used to load text file datanumpy.genfromtxt( ) - Used to lo
3 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
Difference between Numpy array and Numpy matrix While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using
3 min read
Change the Data Type of the Given NumPy Array NumPy arrays are homogenous, meaning all elements in a NumPy array are of the same data type and referred to as array type. You might want to change the data type of the NumPy array to perform some specific operations on the entire data set. In this tutorial, we are going to see how to change the d
4 min read
Python | dtype object length of Numpy array of strings In this post, we are going to see the datatype of the numpy object when the underlying data is of string type. In numpy, if the underlying data type of the given object is string then the dtype of object is the length of the longest string in the array. This is so because we cannot create variable l
3 min read