Calculate mean of list elements inside and outside of range

Issue

I have the following dataframe:

import pandas as pd
mydict = {'ref_start': {0: 73, 1: 93, 2: 107, 3:150},
          'ref_end': {0: 74, 1: 95, 2: 109, 3:150}}
mydf = pd.DataFrame(mydict, columns = ["ref_start", "ref_end"])

Which denotes the start and end of my indexes (plus 1, because my "ref_start" / "ref_end" is 1-indexed), in my list:

import numpy as np
import random
randomlist = []
for i in range(0,200):
    n = random.randint(5,60)
    randomlist.append(n)

Now, I want to (1) calculate the mean of elements that are within the range of ref_start and ref_end:

mymean=[]
for i in range(0, len(mydf)):
   if randomlist[mydf.iloc[i]['ref_start']-1] == mydf.iloc[i]['ref_end']-1:
       mymean.append(np.mean(randomlist[mydf.iloc[i]['ref_start']-1]))
   else:
       mymean.append(np.mean(randomlist[mydf.iloc[i]['ref_start']-1:mydf.iloc[i]['ref_end']-1]))

However, how do I get the mean of the ranges outside mydf, ie. from 1 (start) to 72, 75 to 92, 96 to 106, 110 to 149, and 151 to the end?

Solution

I solved it now like this:

elsemean=[]
for i in range(0,len(mydf)):
    if i == 0:
        elsemean.append(np.mean(randomlist[:int(mydf.iloc[i]["ref_start"]-1)]))
    if mydf.iloc[i]["ref_start"] == mydf.iloc[i]["ref_end"]:
        pass
    if i == len(mydf)-1:
        elsemean.append(np.mean(randomlist[int(mydf.iloc[i]["ref_end"]+1):]))
    else:
        i += 1
        elsemean.append(np.mean(randomlist[int(mydf.iloc[i-1]["ref_end"]+1):int(mydf.iloc[i]["ref_start"]-1)]))

Answered By – rororo

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.