Skip to content
Advertisement

get first 2 characters of each index in array in python

enter image description here

I am trying to access the first two letters of each index in a numpy array in python: I have read previous forum of the error “‘int’ object is not subscriptable , I know it;s not a string, but for my work it’s better to be numpy.array or if anyone suggest me with another thing, please help,

Here is my code:

JavaScript

Data to be used in the .dat file:

JavaScript

Advertisement

Answer

You can’t efficiently slice string elements with [:2], but you can use astype to truncate the strings:

JavaScript

The resulting array could be tested against ‘IS’ etc:

JavaScript

Using two where steps:

JavaScript

np.select could probably used as well.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement