在NumPy数组中的字符串中添加前导零 [英] Adding leading zeros to strings in NumPy array

查看:71
本文介绍了在NumPy数组中的字符串中添加前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3维数组,其中填充了字符串,大部分是数字,但也有一些文本.如果一个字符串仅包含一个数字(即1、5),我想为其添加一个零(01、05、14等).我无法使其适用于我的NumPy数组.

I have a 3-dimensional array filled with strings, mostly of numbers but also some text. If a string contains only one digit (ie. 1, 5), I want to add a zero to it (01,05,14,etc.). I can't get it to work for my NumPy array though.

我尝试了其他尝试:

strlist = ['1','2','3','4','5','6','7','8','9']
arr[np.isin(arr, strlist)] = '0' + arr[np.isin(arr, strlist)] 

但这不起作用.有人有提示吗?

But this doesn't work. Does anyone have any tips?

推荐答案

NumPy对于字符串数组具有几个有用的功能.请参阅 NumPy文档.您要查找的功能是 np.core.defchararray.zfill 或其

NumPy has several useful functions for arrays of strings. See NumPy docs on String operations. The function you are looking for is np.core.defchararray.zfill or its alias np.char.zfill.

大卫·巴克的答案中获取一个示例数组:

Taking an example array from David Buck's answer:

>>> import numpy as np
>>> arr = np.array([[['3', '6', '12'],
                     ['0', '1', '3'],
                     ['5', 'T', '8'],
                     ['19', '15', '11']],
                    [['6', '3', '1'],
                     ['10', '10', 'QR'],
                     ['7', '11', '9'],
                     ['12', '13', '11']],
                    [['1', 'G', '3'],
                     ['10', '9', '2'],
                     ['18', '12', '17'],
                     ['6', '1', '10']]])
>>> np.char.zfill(arr, 2)
array([[['03', '06', '12'],
        ['00', '01', '03'],
        ['05', '0T', '08'],
        ['19', '15', '11']],

       [['06', '03', '01'],
        ['10', '10', 'QR'],
        ['07', '11', '09'],
        ['12', '13', '11']],

       [['01', '0G', '03'],
        ['10', '09', '02'],
        ['18', '12', '17'],
        ['06', '01', '10']]], dtype='<U2')

如果要避免在非数字元素上添加零,则可以使用

If you want to avoid adding zeros to elements that are not digits, we can use boolean array indexing and np.core.defchararray.isdigit function or its alias np.char.isdigit:

>>> mask = np.char.isdigit(arr)
>>> mask
array([[[ True,  True,  True],
        [ True,  True,  True],
        [ True, False,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True, False],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True, False,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]]])
>>> arr[mask] = np.char.zfill(arr[mask], 2)
>>> arr
array([[['03', '06', '12'],
        ['00', '01', '03'],
        ['05', 'T', '08'],
        ['19', '15', '11']],

       [['06', '03', '01'],
        ['10', '10', 'QR'],
        ['07', '11', '09'],
        ['12', '13', '11']],

       [['01', 'G', '03'],
        ['10', '09', '02'],
        ['18', '12', '17'],
        ['06', '01', '10']]], dtype='<U2')

这篇关于在NumPy数组中的字符串中添加前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆