pandas: mean of previous 10 rows with fixed window

Question:

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
Asked By: Sushil Kokil

||

Answers:

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
Answered By: mozway

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

Answered By: Abdul Salam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.