简体   繁体   English

如何将字符串值分配给 numpy 数组中的特定行和列?

[英]How to assign a string value to a particular row and column in numpy array?

I have created an empty metrics in python say我在 python 中创建了一个空指标说

metric = np.empty([1,6])

Now, I am inserting some values to its particular row and column as shown below现在,我正在向其特定的行和列插入一些值,如下所示

metric[0:1,1:2] = 1.23
metric[0:1,2:3] = 2.4
metric[0:1,3:4] = 20
metric[0:1,4:5] = 10
metric[0:1,5:6] = 0.56
metric[0:1,6:7] = 0.50

Now, apart from the integer value, I want to insert a string to the 6th column of 1st row.现在,除了整数值,我想在第一行的第 6 列中插入一个字符串。 ie IE

metric[0:1,6:7] = str('5') + "Days"

which I want to insert "5 Days" in the 6th column of 1st row.我想在第一行的第 6 列中插入“5 天”。 How to do this??这该怎么做?? please suggest...请建议...

This is because of dtype, default dtype for np array is float.这是因为 dtype,np 数组的默认 dtype 是 float。 you need to set it to object.您需要将其设置为对象。

import numpy as np

metric = np.empty([1, 7], dtype=object)

metric[0,1] = 1.23
metric[0,2] = 2.4
metric[0,3] = 20
metric[0,4] = 10
metric[0,5] = 0.56
metric[0,6] = 'Days 0.50'

print(metric)

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

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