Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mean calculation in pandas excluding zeros

Tags:

python

pandas

Is there a direct way to calculate the mean of a dataframe column in pandas but not taking into account data that has zero as a value? Like a parameter inside the .mean() function? Was currently doing it like this:

x = df[df[A]!=0] x.mean() 
like image 952
Gabriel Avatar asked Oct 19 '15 14:10

Gabriel


People also ask

Does mean in pandas ignore NaN?

pandas mean() Key PointsBy default ignore NaN values and performs mean on index axis.

How do you calculate mean of data in pandas?

To find mean of DataFrame, use Pandas DataFrame. mean() function. The DataFrame. mean() function returns the mean of the values for the requested axis.

What does 0 mean in pandas?

In Pandas: axis=0 means along "indexes". It's a row-wise operation.


2 Answers

It also depends on the meaning of 0 in your data.

  • If these are indeed '0' values, then your approach is good
  • If '0' is a placeholder for a value that was not measured (i.e. 'NaN'), then it might make more sense to replace all '0' occurrences with 'NaN' first. Calculation of the mean then by default exclude NaN values.

    df = pd.DataFrame([1, 0, 2, 3, 0], columns=['a']) df = df.replace(0, np.NaN) df.mean() 
like image 62
tibi3000 Avatar answered Sep 20 '22 16:09

tibi3000


df[df["Column_name"] != 0]["Column_name"].mean() 

or if your column name does not contain space char

df[df.Column_Name != 0].Column_Name.mean() 

hopefully it can be included as a parameter in the next "mean" object version

.mean(exclude=0)  #wondering in next versions 
like image 29
nuri Avatar answered Sep 18 '22 16:09

nuri