简体   繁体   中英

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

I have created an empty metrics in python say

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. ie

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

which I want to insert "5 Days" in the 6th column of 1st row. How to do this?? please suggest...

This is because of dtype, default dtype for np array is 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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