

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Encode string array values in Numpy
To encode string array values, use the numpy.char.encode() method in Python Numpy. The arr is the input array to be encoded. The "encoding" parameter sets the name of the encode. The set of available codecs comes from the Python standard library, and may be extended at runtime. The type of the result will depend on the encoding specified.
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of string −
arr = np.array(['zbellazz' 'zztoMzzz' 'zzjohnzz' 'zzkatEzz' 'zzamyzzz' 'zzbradzz'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...\n",arr.size)
To encode string array values, use the numpy.char.encode() method in Python Numpy. The arr is the input array to be encoded. The "encoding" parameter sets the name of the encode −
print("\nResult (encode)...\n",np.char.encode(arr, encoding='cp037'))
Example
import numpy as np # Create a One-Dimensional array of string arr = np.array(['zbellazz' 'zztoMzzz' 'zzjohnzz' 'zzkatEzz' 'zzamyzzz' 'zzbradzz']) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size) # To encode string array values, use the numpy.char.encode() method in Python Numpy # The arr is the input array to be encoded # The "encoding" parameter sets the name of the encode print("\nResult (encode)...\n",np.char.encode(arr, encoding='cp037'))
Output
Array... ['zbellazzzztoMzzzzzjohnzzzzkatEzzzzamyzzzzzbradzz'] Array datatype... <U48 Array Dimensions... 1 Our Array Shape... (1,) Elements in the Array... 1 Result (encode)... [b'\xa9\x82\x85\x93\x93\x81\xa9\xa9\xa9\xa9\xa3\x96\xd4\xa9\xa9\xa9\xa9\xa9\x91\x96\x88\x95\xa9\xa9\xa9\xa9\x92\x81\xa3\xc5\xa9\xa9\xa9\xa9\x81\x94\xa8\xa9\xa9\xa9\xa9\xa9\x82\x99\x81\x84\xa9\xa9']
- Related Questions & Answers
- To decode string array values that is already encoded in Numpy
- Test array values for finiteness in Numpy
- Test array values for NaN in Numpy
- How to encode the string in android?
- How to encode a string in JavaScript?
- Encode String with Shortest Length in C++
- Clip (limit) the values in a Numpy array
- Copy values from one array to another in Numpy
- How to URL encode a string (NSString) in iPhone?
- Test array values for positive or negative infinity in Numpy
- Test array values for NaT (not a time) in Numpy
- PHP – Encode string for MIME header using mb_encode_mimeheader()
- Append values to the end of a masked array in Numpy
- Return the fractional and integral parts of array values in Numpy
- Encode Number in C++