【问题标题】:convert dtype object with tuples into np.array将带有元组的 dtype 对象转换为 np.array
【发布时间】:2023-03-16 19:41:01
【问题描述】:

我有一个 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.

【问题讨论】:

    标签: python


    【解决方案1】:
    np.array(e.names)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2014-04-03
      • 2023-02-15
      相关资源
      最近更新 更多