Python Forum

Full Version: How to efficiently average same entries of lists in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am working on data from which I calculate (using peakutilis) 4 peaks.
These positions of the 4 peaks are stored in a list like:
[3141, 4989, 5499, 6714]

I have over a hundred nearly similar data sets, and I want to efficiently calculate the average of each entry position (entry in the list)

For now I am doing it like this:

indexlist = []
for t, scope_y in enumerate(scope_ys):
    scope_y = scope_y[0]
    indexes = peakutils.indexes(scope_y, thres=0.07, min_dist=500)
    indexlist.append(indexes)

v = np.asarray(indexlist)
a_lst = []
b_lst = []
c_lst = []
d_lst = []

for i in range(len(v)):
    a = v[i,0]
    a_lst.append(a)
    b = v[i,1]
    b_lst.append(b)
    c = v[i,2]
    c_lst.append(c)
    d = v[i,3]
    d_lst.append(d)
for i in range(4):
    a_avg = int(np.average(a_lst))
    b_avg = int(np.average(b_lst))
    c_avg = int(np.average(c_lst))
    d_avg = int(np.average(d_lst))
indexes = [a_avg,b_avg,c_avg,d_avg]
print(indexes) 
Do you know how to do this in a more efficient way?

Regards,
xquad
Do you mean like the average from the every item on the same position from the lists? Or in other words, the average from all first items for example?
The first case: The average from every item on the same position from the lists.
And with efficiently I mean fewer lines of codes and if its possible also in a way that is takes less computation into account.
(Dec-17-2021, 03:54 PM)xquad Wrote: [ -> ]The first case: The average from every item on the same position from the lists.

Have you tried something with pandas or something already? Maybe you can convert al lists to a pandas dataframe. From then it should be quite efficient (at least in code), to get the average from one column.

Maybe this post can help you out: https://stackoverflow.com/questions/3103...erage-mean
Havent worked with pandas yet. Its pretty handy! Thanks for the recom.
The code shrinks down to:

indexlist = []
for t, scope_y in enumerate(scope_ys):
    scope_y = scope_y[0]
    indexes = peakutils.indexes(scope_y, thres=0.07, min_dist=500)
    indexlist.append(indexes)

df = pd.DataFrame.from_records(indexlist)
average = np.ones(4)
for i in range(len(indexes)):
    average[i] = int(np.average(df.iloc[:,i]))
Te data type is a np array and looks like this: [3141. 4989. 5499. 6714.] such that in a subsequent for loop
for i, laser in enumerate(average):
the error occurs:
"slice indices must be integers or None or have an __index__ method"