-3

I have an array of 19000 numbers from 0 to 1 that represent percentages and I would like to have a database of yes and no where yes is every number above 0.3 and no every number below 0.3.

Something that should look like this where original is my initial file

original = [0.2499320, 0.456484, 0.324824 ... 0.231, 0.3213]

modified = [no, yes, yes... no, yes]

Is there a way to do this? Thank you in advance

AAAA
  • 23
  • 3
  • Can you include example input data and expected output to provide a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Michael Szczesny Nov 25 '20 at 15:14
  • I edited the post to make it more understandable, sorry I am new – AAAA Nov 25 '20 at 15:23

1 Answers1

1

numpy.where will do the job.

import numpy as np

arr = np.random.rand(10)

print(arr)
print(np.where(arr>=0.3, 'Yes', 'No'))

# [0.23556319 0.1173074  0.2673033  0.05552573 0.79930567 0.33443317
# 0.88644862 0.89914459 0.64551288 0.60601345]
# ['No' 'No' 'No' 'No' 'Yes' 'Yes' 'Yes' 'Yes' 'Yes' 'Yes']
Roman Zh.
  • 985
  • 2
  • 6
  • 20
  • It gives me an error "TypeError: '>=' not supported between instances of 'list' and 'float'" – AAAA Nov 25 '20 at 15:33
  • It is because `list` doesn't support it, while `numpy.array` does. Cast your list to the `numpy.array`: `arr = np.array(your_list)` and here you go – Roman Zh. Nov 27 '20 at 10:57