2

I have a list:

first = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]

I want another list with mean of three values and so the new list be:

new = [2,5,8,11,14,17]

There will be only 6 values in the new list as there are only 18 elements in the first.

I am looking for an elegant way to do this with a minimal number of steps for a large list.

Georgy
  • 12,464
  • 7
  • 65
  • 73
DearBeliever
  • 41
  • 1
  • 7
  • 1
    will the number of elements in `first` always be divisible by the window size? – Daniel F Jan 13 '20 at 07:16
  • Does this answer your question? [The average value of a list, in chunks of 100 items](https://stackoverflow.com/questions/53373362/the-average-value-of-a-list-in-chunks-of-100-items) – Georgy Jan 14 '20 at 17:19
  • For NumPy: [Average every x numbers in NumPy array](https://stackoverflow.com/q/41815361/7851470) – Georgy Jan 14 '20 at 17:22

4 Answers4

8

Using numpy, you can reshape your list of 18 elements into an array of shape (6, 3) and then take the mean over the rows

import numpy as np
a = np.array(first)

>>> a.reshape(-1, 3)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],

>>> a.reshape(-1, 3).mean(axis=1)
array([ 2.,  5.,  8., 11., 14., 17.])

The use of -1 in np.reshape(-1, 3) actually allows you to use this approach for any array whose size is a multiple of 3 and it will automatically size the first dimension appropriately

sshashank124
  • 31,495
  • 9
  • 67
  • 76
6

Heres a solution using pandas with groupby:

import pandas as pd

ser = pd.Series(first)
ser.groupby(ser.index//3).mean()

0     2
1     5
2     8
3    11
4    14
5    17
dtype: int64
Erfan
  • 40,971
  • 8
  • 66
  • 78
5

You can take a slice of first using for loop that iterates in 3 interval

import statistics

new = [statistics.mean(first[i:i + 3]) for i in range(0, len(first), 3)]
print(new) # [2, 5, 8, 11, 14, 17]
Guy
  • 46,488
  • 10
  • 44
  • 88
4

Here's another solution using statistics.mean() to get the mean of each chunk of every three numbers in the list.

>>> from statistics import mean
>>> first = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
>>> [mean(x) for x in zip(*[iter(first)] * 3)]
[2, 5, 8, 11, 14, 17]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75