Home > Back-end >  pandas: mean of previous 10 rows with fixed window
pandas: mean of previous 10 rows with fixed window

Time:10-15

Just I created sampled dataframe. Need to measure the mean of previous 10 values and get that value in another column other rows shows nan.

np.random.seed(42)
sp= sorted(np.random.randint(0,200,55))
odo = sorted(np.random.randint(0,40,55))
df1 = pd.DataFrame({'sp':sp,'odo':odo})

# Getting rolling mean of 10 values
df2 =df1.rolling(10).agg('mean')

# It is giving result continuous

# I want below to result able with indexing.
df3 = df2[9::10]

Expected result like this

       sp   odo
9    17.7   2.9
19   56.2   8.2
29   89.1  15.4
39  120.7  23.4
49  167.9  30.7

CodePudding user response:

You don't want a rolling.mean but a groupby.mean by chunks of 10 rows:

df1.groupby(np.arange(len(df1))//10).mean()

To have the same indices as you showed:

n = 10
df1.groupby((np.arange(len(df1))//n 1)*n-1).mean()

Output:

       sp   odo
9    17.7   2.9
19   56.2   8.2
29   89.1  15.4
39  120.7  23.4
49  167.9  30.7
59  189.2  37.2

For only groups with 10 values:

n = 10
l = len(df1)//n*n
df1.iloc[:l].groupby((np.arange(l)//n 1)*n-1).mean()

Output:

       sp   odo
9    17.7   2.9
19   56.2   8.2
29   89.1  15.4
39  120.7  23.4
49  167.9  30.7

CodePudding user response:

You might as well Use "from statistics import mean" And also using the random function doesn't seem necessary You could just use np.arange function

  • Related