How can I slice each element of a numpy array of strings?

前端 未结 4 2019
面向向阳花
面向向阳花 2020-12-01 16:25

Numpy has some very useful string operations, which vectorize the usual Python string operations.

Compared to these operation and to pandas.str, the num

4条回答
  •  情歌与酒
    2020-12-01 17:12

    To solve this, so far I've been transforming the numpy array to a pandas Series and back. It is not a pretty solution, but it works and it works relatively fast.

    a = numpy.array(['hello', 'how', 'are', 'you'])
    pandas.Series(a).str[1:3].values
    array(['el', 'ow', 're', 'ou'], dtype=object)
    

提交回复
热议问题