Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas: mean of previous 10 rows with fixed window

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading