- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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']
- Related Articles
- 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
- Clip (limit) the values in a Numpy array
- How to encode a string in JavaScript?
- How to encode the string in android?
- Encode String with Shortest Length in C++
- Copy values from one array to another in Numpy
- Test array values for positive or negative infinity in Numpy
- Test array values for NaT (not a time) in Numpy
- How to URL encode a string (NSString) in iPhone?
- 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
- Return array of indices of the maximum values from a masked array in NumPy
