简体   繁体   English

关于将字符串列表转换为整数的问题

[英]Question about converting a list of strings to ints

I am trying to convert one column in my numpy array from strings to ints, however when I tried the methods I found online, it didn't mutate the column.我正在尝试将 numpy 数组中的一列从字符串转换为整数,但是当我尝试在网上找到的方法时,它并没有改变该列。 Here is my code:这是我的代码:

frequencies = np.array([['a', '24273'],
               ['b', '11416'],
               ['c', '8805'],
               ['d', '6020']])
               frequencies[:, 1] = frequencies[:, 1].astype(int)
               print(frequencies)

The array is unchanged when I print the output.当我打印 output 时,数组没有改变。

You should assert the datatype for the frequencies array to be dtype=object to accomodate for different data types;您应该将frequencies数组的数据类型断言为dtype=object以适应不同的数据类型; int s and string s. int s 和string s。

import numpy as np 

frequencies = np.array([['a', '24273'],
               ['b', '11416'],
               ['c', '8805'],
               ['d', '6020']],dtype=object)
frequencies[:, 1] = frequencies[:, 1].astype(int)
print(frequencies)

[['a' 24273]
 ['b' 11416]
 ['c' 8805]
 ['d' 6020]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM