简体   繁体   中英

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]

You have an array of floats. You want to put a string value to an element of that array. It is not possible.

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?

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