Mass17
Mass17

Reputation: 1605

Python: percentage of rows in the dataset

I am new to the python and numpy. I want to find the % of time a threshold value occurs in the data set. I have created a function to do this task. I have extracted the data using numpy,

datanew2 = np.array(data[:,4]) # this has 600 elements

def func1():
    x = float(input("Max threshold value: "))
    for i in range(600):
        if datanew2[i] >= x:
            A = datanew2[i]
    print(A.shape)

func1()

when I call this func1, I get the following;

()

I want to know how many times in my dataset (datanew2), the threshold value is less that of elements in datanew2, so that I can find the % of occurrence.

Thank you for suggestion.

Upvotes: 0

Views: 63

Answers (1)

Space Impact
Space Impact

Reputation: 13255

Use array comaparison then sum the array as:

a = np.arange(10).reshape(2,5)
print(a)
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

threshold=5
(a>=threshold).sum()

5

Upvotes: 1

Related Questions