问题出在您的数组的 dtype('<U6')
。
>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']])
>>> data.dtype
dtype('<U6')
>>>
它将自动截断:
>>> data[0,0] = "123456789"
>>> data
array([['123456', 'Weight'],
['165', '48'],
['168', '50'],
['173', '53']],
dtype='<U6')
>>>
当你创建数组时,你总是可以将dtype指定为'object',但这会减少numpy
的许多速度优势。
或者,你可以指定一个更长的字符串类型:
>>> data
array([['Height', 'Weight'],
['165', '48'],
['168', '50'],
['173', '53']],
dtype='<U20')
>>> data[0,0]='Height_1'
>>> data
array([['Height_1', 'Weight'],
['165', '48'],
['168', '50'],
['173', '53']],
dtype='<U20')
>>>
但要小心,如果您将限制设置得过长,会浪费内存:
>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U20')
>>> data.nbytes
800
>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U6')
>>> data.nbytes
240
如果你只需要有限数量的字符,请考虑使用字节串(只需占用1/4的内存):
>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='S20')
>>> data.nbytes
200
>>>