imran khan
imran khan

Reputation: 105

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. i.e.

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

Upvotes: 2

Views: 634

Answers (1)

Shiva Burade
Shiva Burade

Reputation: 365

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)

Upvotes: 1

Related Questions