Home > Software design >  how to replace an element of a 2-D numpy.array with a string in python?
how to replace an element of a 2-D numpy.array with a string in python?

Time:07-19

I want to change the elements of array 2-D under the next condition: if the element> 100 change this element for the word "True" else change the element for the word: "False". I tried to do it but I couldn't. I share the next code that i did. I hope you can helpe me please.

import numpy as np
h1 =np.array([[10, 85, 103, 444, 150, 200, 35, 47],      
                 [532, 476, 0, 1011, 50, 674, 5, 999],
                 [985, 7, 99, 101, 1, 58, 300, 78],
                 [750, 649, 86, 8, 505, 41, 745, 187]])
for r in range (0,len(h1)):
    for c in range(0,len(h1[0])):
        valor = h1[r][c]
        if valor > 100:
            h1[r][c] = 'True'
        else:
            h1[r][c] = 'False'

Theorically the out should be:

[[False, False,  True,  True,  True,  True, False, False],
[ True,  True, False,  True, False,  True, False,  True],
[ True, False, False,  True, False, False,  True, False],
[ True,  True, False, False,  True, False,  True,  True]]

CodePudding user response:

You don't need loops, h1 = h1 > 100 will do the job

[[False False  True  True  True  True False False]
 [ True  True False  True False  True False  True]
 [ True False False  True False False  True False]
 [ True  True False False  True False  True  True]]

If you really want the values as strings use astype(str)

h1 = (h1 > 100).astype(str)

CodePudding user response:

Why for-loop?! When your array is numpy.array. you can use numpy.where. If you have specific string you can use numpy.where and set any string that you like.

import numpy as np
h1 =np.array([[10, 85, 103, 444, 150, 200, 35, 47],      
                 [532, 476, 0, 1011, 50, 674, 5, 999],
                 [985, 7, 99, 101, 1, 58, 300, 78],
                 [750, 649, 86, 8, 505, 41, 745, 187]])

# as Boolean : np.where(h1>100, True, False)
np.where(h1>100, 'True', 'False')

But if Only you want to use 'True' or 'False' you can use astype(str):

>>> (h1>100).astype(str)
array([
    ['False', 'False', 'True', 'True', 'True', 'True', 'False','False'],
    ['True', 'True', 'False', 'True', 'False', 'True', 'False','True'],
    ['True', 'False', 'False', 'True', 'False', 'False', 'True','False'],
    ['True', 'True', 'False', 'False', 'True', 'False', 'True','True']], 
    dtype='<U5')

CodePudding user response:

You can do this:

h2 = []
for i in h1: 
    x = [True if x >100 else False for x in i]
    h2.append(x)

But as the above comments mentioned there are much easier ways.

CodePudding user response:

I'mahdi's solution is the best I would say, but if you want to keep doing it in for-loops:

The reason it does not work for you is, that your h1 is a 2D array of integers. If you instead say

h1 =np.array([[BLABLABLA]],dtype = object))

then you can alter your array. Be careful tho, currently you are setting the elements with the string

"True"

and not with the actual bool

True

so I would maybe not use the " ". If you then want to be supercool and make sure, that h1 is an array of bools, I would recommend to also do a line saying

h1 = h1.astype(bool)

Hope this helps. If you have any questions, let me know

  • Related