Average calculation in pandas excluding zeros

Is there a direct way to calculate the average value of a dataframe column in pandas, but not taking into account data that has zero as the value? How is the parameter inside the .mean () function? Currently, this is done as follows:

x = df[df[A]!=0]
x.mean()
+9
source share
2 answers

It also depends on the value 0 in your data.

  • If these are really "0" then your approach is good.
  • If “0” is a placeholder for a value that has not been measured (ie, “NaN”), then it may make sense to replace all “0” occurrences first with “NaN”. Calculation of the average value by default excludes NaN values.

    df = pd.DataFrame([1, 0, 2, 3, 0], columns=['a'])
    df = df.replace(0, np.NaN)
    df.mean()
    
+15

, df['A'].replace(0, np.NaN).mean() , df['A'].mean(exclude=0)?

0

Source: https://habr.com/ru/post/1612277/


All Articles