Kostas Belivanis Kostas Belivanis - 1 year ago 81
Python Question

How can I put a string to an array

I want to put a string to an array location but I get an error:

ValueError: could not convert string to float

My code is the following:

k = np.ceil(99/8)

rs = np.zeros((int(k), 10))

for i in range(0, int(k)):
rs[i, 0] = "FREQ"
for j in range(1,9):
rs[i, j] = rs_imp[8*k+j, 0]

Answer Source

Your array is implicitly a float array, but you can change the data type to object to be able to include both floats and strings:

rs = np.zeros((int(k), 10), dtype='object')

But this is going to rob you of potential optimizations and may cause unexpected problems later on.

Sounds like an XY problem. Why do you think you need to add the string "FREQ" into this array? What are you really trying to do?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download