【问题标题】:create structured numpy array in python with strings and int使用字符串和int在python中创建结构化的numpy数组
【发布时间】:2018-06-30 23:17:41
【问题描述】:

我有这个:

>>> matriz
      [['b8:27:eb:d6:e3:10', '0.428s', '198'],
      ['b8:27:eb:d6:e3:10', '0.428s', '232'],
      ['b8:27:eb:07:65:ad', '0.796s', '180'], 
      ['b8:27:eb:07:65:ad', '0.796s', '255'],
      dtype='<U17']`

但我需要专栏

   `matriz[:, [2]] : 
    [['198'],
     ['232'],
     ['180'], 
     ['255']]` 

作为 int 和其他列作为字符串,我正在尝试使用结构化的 numpy 数组,但我有这个错误消息,

 ValueError: invalid literal for int() with base 10: 'b8:27:eb:d6:e3:10'
 TypeError: a bytes-like object is required, not 'str'

我用过

  matriz=np.array(matriz, dtype='U17,U17,i4')

我正在为树莓派 3 使用 numpy 版本“1.12.1”,我不知道我做错了什么。 非常感谢

【问题讨论】:

  • 您确实应该将数据发布为文本,而不是作为图像
  • 另外你为什么想要这样做,如果你想要混合数据类型你应该使用pandas
  • 感谢您的更正,是的,我需要混合这些数据,因为我想知道列 matriz[:, [2]] 中的哪些数据用于列 matriz[:, [0]]
  • 你知道我能不能用 pandas 做到这一点?

标签: python arrays python-3.x numpy


【解决方案1】:
In [484]: x = np.array([['b8:27:eb:d6:e3:10', '0.428s', '198'],
     ...:               ['b8:27:eb:d6:e3:10', '0.428s', '232'],
     ...:               ['b8:27:eb:07:65:ad', '0.796s', '180'], 
     ...:               ['b8:27:eb:07:65:ad', '0.796s', '255']],
     ...:              dtype='<U17')
     ...:              

您可以通过astype 转换获取最后一列:

In [485]: x[:,2].astype(int)
Out[485]: array([198, 232, 180, 255])
In [486]: x[:,[2]].astype(int)
Out[486]: 
array([[198],
       [232],
       [180],
       [255]])

要构造结构化数组,您需要提供一个元组列表。具有复合 dtype 的列表或非结构化数组的列表将产生您的错误。

In [487]: np.array([tuple(i) for i in x],'U17,U10,int')
Out[487]: 
array([('b8:27:eb:d6:e3:10', '0.428s', 198),
       ('b8:27:eb:d6:e3:10', '0.428s', 232),
       ('b8:27:eb:07:65:ad', '0.796s', 180),
       ('b8:27:eb:07:65:ad', '0.796s', 255)],
      dtype=[('f0', '<U17'), ('f1', '<U10'), ('f2', '<i8')])
In [488]: _['f2']
Out[488]: array([198, 232, 180, 255])

结构化数组的字段按名称获取。

【讨论】:

    【解决方案2】:

    NumPy 最适用于同构 dtype 数组。如果您有不同的类型,Pandas 是一个不错的选择。

    但是,您的问题可以通过 NumPy structured arrays

    import numpy as np
    
    x = np.array([['b8:27:eb:d6:e3:10', '0.428s', '198'],
                  ['b8:27:eb:d6:e3:10', '0.428s', '232'],
                  ['b8:27:eb:07:65:ad', '0.796s', '180'], 
                  ['b8:27:eb:07:65:ad', '0.796s', '255']],
                 dtype='<U17')
    
    arr = np.core.records.fromarrays(x.transpose(),
                                     formats='<U17,<U17,i4',
                                     names='col1,col2,col3')
    
    print(arr)
    
    rec.array([('b8:27:eb:d6:e3:10', '0.428s', 198),
               ('b8:27:eb:d6:e3:10', '0.428s', 232),
               ('b8:27:eb:07:65:ad', '0.796s', 180),
               ('b8:27:eb:07:65:ad', '0.796s', 255)],
              dtype=[('col1', '<U17'), ('col2', '<U17'), ('col3', '<i4')])
    

    【讨论】:

    • 非常感谢,我需要混合这些数据,因为我想知道列 arr[:, [2]] 中的哪些数据用于列 arr[:, [0]]。你知道我是否可以通过这种方式做到这一点?谢谢你...
    • arr['col3'] 是您访问结构化数组字段的方式。
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多