我有一个3维数组,其中填充了字符串,大部分是数字,但也有一些文本。如果一个字符串仅包含一个数字(即1、5),我想为其添加一个零(01、05、14等)。我不能让它为我的NumPy数组工作。
我尝试了其他尝试:
strlist = ['1','2','3','4','5','6','7','8','9']
arr[np.isin(arr, strlist)] = '0' + arr[np.isin(arr, strlist)]
但这不起作用。有人有提示吗?
答案 0 :(得分:4)
NumPy具有一些有用的函数,用于字符串数组。参见NumPy docs on String operations。您要查找的功能是np.core.defchararray.zfill
或其alias np.char.zfill
。
从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')
如果要避免在非数字元素上添加零,则可以使用boolean array indexing和np.core.defchararray.isdigit
函数或其别名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')
答案 1 :(得分:2)
您可以定义一个函数,该函数对整数/非整数进行填充,然后使用向量化将其应用于整个数组。
import numpy as np
def pad(value):
try:
return '{0:0>2}'.format(int(value))
except:
return value
vfunc = np.vectorize(pad)
arr = vfunc(arr)
print(arr)
将其应用于以下输入:
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']],])
返回
[[['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']]]