Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'mean' is not defined

Tags:

python

Calculating the basic statistics, I get the following working well:

import pandas as pd    

max(df[Price]) 
min(df[Price]) 

But, this is returning an error:

mean(df[Price]) 

NameError: name 'mean' is not defined

I'm just trying to understand the logic of this.

This one works well:

df[Price].mean()

What kind of statistics work after the dot and which ones must wrap the column?


1 Answers

min() and max() are functions provided as Python built-ins.

You can use them on any iterable, which includes Pandas series, which is why what you're doing works.

Pandas also provides .min() and .max() as methods on series and dataframes, so e.g. df["Price"].min() would also work. The full list of Series functions is here; the full list of DataFrame functions is here.

If you do want to use a free function called mean(), e.g. when you have something that's not a Pandas series and you don't want to convert it to one, one actually does exist in the Python standard library, but you will have to import it:

from statistics import mean
like image 95
AKX Avatar answered Sep 23 '25 01:09

AKX