- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a recarray from a list of records in text form and set a valid datatype in Numpy
To create a recarray from a list of records in text form, use the numpy.core.records.fromrecords() method in Python Numpy. The names is set using the "names" parameter. The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used. The datatype is set using the "dtype" parameter.
The first parameter is the data in the same field may be heterogeneous - they will be promoted to the highest data type. The dtype is the valid dtype for all arrays. The formats, names, titles, aligned, byteorder parameters, f dtype is None, these arguments are passed to numpy.format_parser to construct a dtype. If both formats and dtype are None, then this will auto-detect formats. Use list of tuples rather than list of lists for faster processing.
Steps
At first, import the required library −
import numpy as np
Create a new array using the numpy.array() method −
arr1 = np.array([[7, 14, 21], [30, 37, 45]]) arr2 = np.array([[11.3, 18.7, 24], [87.5, 65, 23.8]]) arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']])
Display the arrays −
print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3)
Get the type of the arrays −
print("
Array1 type...
", arr1.dtype) print("
Array2 type...
", arr2.dtype) print("
Array3 type...
", arr3.dtype)
Get the dimensions of the Arrays −
print("
Array1 Dimensions...
", arr1.ndim) print("
Array2 Dimensions...
", arr2.ndim) print("
Array3 Dimensions...
", arr3.ndim)
To create a recarray from a list of records in text form, use the numpy.core.records.fromrecords() method in Python Numpy. The datatype is set using the "dtype" parameter −
print("
Record Array...
",np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2, col3', dtype=np.dtype(('a', np.int32), ('b', np.float32), ('c','S3' ))))
Example
import numpy as np # Create a new array using the numpy.array() method arr1 = np.array([[7, 14, 21], [30, 37, 45]]) arr2 = np.array([[11.3, 18.7, 24], [87.5, 65, 23.8]]) arr3 = np.array([['12', 'bbb', 'john'], ['5.6', '29', 'k']]) # Display the arrays print("Array1...
",arr1) print("Array2...
",arr2) print("Array3...
",arr3) # Get the type of the arrays print("
Array1 type...
", arr1.dtype) print("
Array2 type...
", arr2.dtype) print("
Array3 type...
", arr3.dtype) # Get the dimensions of the Arrays print("
Array1 Dimensions...
", arr1.ndim) print("
Array2 Dimensions...
", arr2.ndim) print("
Array3 Dimensions...
", arr3.ndim) # To create a recarray from a list of records in text form, use the numpy.core.records.fromrecords() method in Python Numpy # The names is set using the "names" parameter # The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. # An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used. # The datatype is set using the "dtype" parameter print("
Record Array...
",np.core.records.fromrecords([arr1,arr2,arr3], names = 'col1, col2, col3', dtype=np.dtype(('a', np.int32), ('b', np.float32), ('c','S3' ))))
Output
Array1... [[ 7 14 21] [30 37 45]] Array2... [[11.3 18.7 24. ] [87.5 65. 23.8]] Array3... [['12' 'bbb' 'john'] ['5.6' '29' 'k']] Array1 type... int64 Array2 type... float64 Array3 type... <U4 Array1 Dimensions... 2 Array2 Dimensions... 2 Array3 Dimensions... 2 Record Array... [[[b'\x07\x00\x00\x00' b'\x0E\x00\x00\x00' b'\x15\x00\x00\x00'] [b'\x1E\x00\x00\x00' b'\x25\x00\x00\x00' b'\x2D\x00\x00\x00']] [[b'\x9A\x99\x99\x99' b'\x33\x33\x33\x33' b'\x00\x00\x00\x00'] [b'\x00\x00\x00\x00' b'\x00\x00\x00\x00' b'\xCD\xCC\xCC\xCC']] [[b'\x31\x00\x00\x00' b'\x62\x00\x00\x00' b'\x6A\x00\x00\x00'] [b'\x35\x00\x00\x00' b'\x32\x00\x00\x00' b'\x6B\x00\x00\x00']]]
To Continue Learning Please Login
Login with Google