问题
I would like to count the occurrences of "1" in the sliced array of "col3".
"y = (arr[row-x:row2]).sum()" is just an example that the codes run! Here i obtain something like:
"y = (arr[row-x:row, 2]).count(1)" to count a dynamic array of row 2, how often "1" occurred.
#Stackoverflow example
from numba import njit
import pandas as pd
d = {'col1': [20, 23, 25, 44, 46, 47, 48, 49, 50, 50, 52, 55, 56, 69, 70],
'col2': [39, 32, 42, 50, 63, 67, 64, 68, 68, 74, 59, 75, 58, 71, 66],
'col3': [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1]}
df = pd.DataFrame(data=d)
df["overlap_count"] = 0 #create new column
df["count"] = 0 #create new column
n = 3 #if x >= n, then value = 0
n1 = 2
a = df[['col1','col2','col3','overlap_count', 'count']].values
@njit
def custom_sum(arr, n):
for row in range(arr.shape[0]):
x = (arr[0:row, 1] > arr[row, 0]).sum()
y = (arr[row-x:row, 2]).sum()
#y = (arr[row-x:row, 2]).count(1) #this is what I tried!
arr[row, 3] = x+1
arr[row, 4] = y+1
if x >= n:
arr[row, 1] = 0
arr[row, 3] = 0
if y >= n1:
arr[row, 4] = 0
return arr
df1 = pd.DataFrame(custom_sum(a, n), columns=df.columns)
print (df1)
I obtain in the column "count", for example in column "count"
row 0 = 1, row 1 = 2, row 2 = 0 (because there are more than 2 (n1) occurrences)
来源:https://stackoverflow.com/questions/61078906/numba-how-i-can-count-occurrences-in-an-numba-loop