[FIXED] Python average exclude some rows

Issue

When i use .mean in python, it calculates the mean of the all rows.

What if I want to calculate the mean excluding the first few rows of the data ?

id='dv3'>

Solution

What I am able to understand from your question is that you have a 2d numpy array and want to calculate the mean of the rows except the first few.

You can use list slicing and reassign it to some other variable

b = a[n:]

Where new list if from nth element till last, including n.

Example:

import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a[1:]
print(a.mean(), b.mean())

Output:

2.5 3.5


Answered By - chirag goyal
Answer Checked By - David Goodson (Python.Easybugfix Volunteer)

Related Posts

Post a Comment