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...
",arr)

Get the datatype −

print("
Array datatype...
",arr.dtype)

Get the dimensions of the Array −

print("
Array Dimensions...
",arr.ndim)

Get the shape of the Array −

print("
Our Array Shape...
",arr.shape)

Get the number of elements of the Array −

print("
Elements in the Array...
",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("
Result (encode)...
",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...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",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("
Result (encode)...
",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']

Advertisements