首页 > 解决方案 > 如何将数字 numpy 数组更改为 char 数组?

问题描述

我想使用 python 将数字 numpy 数组更改为 char 数组,例如,

a = np.arange(25).reshape([5,5])

如何将数组 a 更改为 char 数组?

标签: pythonnumpy

解决方案


您可以使用方法更改 numpy 数组的数据类型astype。试试下面的例子:

import numpy as np

a = np.arange(25).reshape([5,5])
a.dtype  #check data type of array it should show int


new = a.astype(str)  #change data type of array to string
new.dtype  #Check the data type again it should show string

推荐阅读