将带有元组的dtype对象转换为np.array

时间:2016-05-06 21:52:21

标签: python

我有一个dtype对象e

>>> e

dtype([('cards', 'O'), ('store', 'O'), ('march', 'O'),
     ('fam', 'O'), ('cup', 'O'), ('sex', 'O'), ('educ', 'O'),  
     ('age', 'O'), ('handedness', 'O')])

我想把它变成一个字符串数组:

np.array(['cards','store','march','fam','cup','sex','educ','age','handedness'])

有没有办法将此对象转换为如图所示的数组?

我在这里尝试过这些建议:

How to convert a Numpy 2D array with object dtype to a regular 2D array of floats

但是我收到了错误:

ValueError                                Traceback (most recent call last)
<ipython-input-160-dc37d6f17f2f> in <module>()
----> 1 np.array(list(e[:, 1]), dtype=str)

ValueError: Field key must be an integer, string, or unicode.

1 个答案:

答案 0 :(得分:0)

np.array(e.names)

dtype对象的names属性是其字段名称的元组,如果它没有字段,则为None