【问题标题】:How to convert numpy array elements from string to int如何将numpy数组元素从字符串转换为int
【发布时间】:2018-07-01 14:18:44
【问题描述】:

我有一个字符串格式的数字列表。我使用 np.asarray() 将该列表转换为 numpy 数组。

如何将字符串元素转换为整数?

【问题讨论】:

标签: python arrays string python-3.x numpy


【解决方案1】:

如果您有x = np.matrix,其中每个元素都是“1.0”(如str)并且您想将其转换为intfloat

x = x.astype(np.float)

【讨论】:

    【解决方案2】:
    import numpy as np
    nums_str = ['1','23','345']
    nums_str_np = np.asarray(nums_str)
    nums_int_np = nums_str_np.astype('int')
    

    nums_int_np - 现在是 np 整数数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      相关资源
      最近更新 更多