I have built a python program processing the probability of various datasets. I input 'manually' various mean values and standard deviations, and that works, however I need to automate it so that I can upload all my data through a text or csv file. I've got so far but now have a nested for loop query I think with indices problems, but some background follows...

My code works for a small dataset where I can manually key in 6-8 parameters working but now I need to automate it and upload various inputs of unknown sizes by csv / text file. I am copying my existing code and amending it where appropriate but I have run into a problem.

I have a 2\_D numpy-array where some probabilities have been reverse sorted. I have a second array which gives me the value of 68.3% of each row, and I want to trim the low value 31.7% data.

I need a solution which can handle an unspecified number of rows.

My pre-existing code worked for a single one-dimensional array was

```
    prob_combine_sum= np.sum(prob_combine)

    #Reverse sort the probabilities
    prob_combine_sorted=sorted(prob_combine, reverse=True)

    #Calculate 1 SD from peak Prob by multiplying Total Prob by 68.3%
    sixty_eight_percent=prob_combine_sum*0.68269

    #Loop over the sorted list and append the 1SD data into a list 
    #onesd_prob_combine
    onesd_prob_combine=[]
    for i in prob_combine_sorted:
    onesd_prob_combine.append(i)
    if sum(onesd_prob_combine) > sixty_eight_percent:
    break

```

That worked. However, now I have a multi-dimensional array, and I want to take the 1 standard deviation data from that multi-dimensional array and stick it in another. 

There's probably more than one way of doing this but I thought I would stick to the for loop, but now it's more complicated by the indices. I need to preserve the data structure, and I need to be able to handle unlimited numbers of rows in the future.

I simulated some data and if I can get this to work with this, I should be able to put it in my program.

```
    sorted_probabilities=np.asarray([[9,8,7,6,5,4,3,2,1], 
    [87,67,54,43,32,22,16,14,2],[100,99,78,65,45,43,39,22,3], 
    [67,64,49,45,42,40,28,23,17]])
    sd_test=np.asarray([30.7215,230.0699,306.5323,256.0125])
    target_array=np.zeros(4).reshape(4,1)

    #Task transfer data from sorted_probabilities to target array on 
    condition that value in each target row is less than the value in the 
    sd_test array.

    #Ignore the problem that data transferred won't add up to 68.3%.
    My real data-sample is very big.  I just need a way of trimmining 
    and transferring.

    for row in sorted_probabilities:
        for element in row:
        target_array[row].append[i]
        if sum(target[row]) > sd_test[row]:
            break

```

Error: IndexError: index 9 is out of bounds for axis 0 with size 4

I know it's not a very good attempt. My problem is that I need a solution which will work for any 2D array, not just one with 4 rows.

I'd be really grateful for any help.

Thank you

Edit:

Can someone help me out with this? I am struggling.

I think the reason my loop will not work is that the 'index' row I am using is not a number, but in this case a row. I will have a think about this. In meantime has anyone a solution? 

Thanks

I tried the following code after reading the comments:

```
    for counter, value in enumerate(sorted_probabilities):

        for i, element in enumerate(value):

        target_array[counter]=sorted_probabilities[counter][element]

        if target_array[counter] > sd_test[counter]:
               break

```

I get an error: IndexError: index 9 is out of bounds for axis 0 with size 9

I think it's because I am trying to add to numpy array of pre-determined dimensions? I am not sure. I am going to try another tack now as I can not do this with this approach. It's having to maintain the rows in the target array that is making it difficult. Each row relates to an object, and if I lose the structure it will be pointless.