Home > OS >  Unable to use mean funcation on list of integers - AttributeError: 'list' object has no at
Unable to use mean funcation on list of integers - AttributeError: 'list' object has no at

Time:10-19

I am learning Python and have been trying out some simple equations using NumPy. While trying to calculate the mean of a list of numbers using the mean() function, I encountered an error.

AttributeError: 'list' object has no attribute 'mean'

P.S To avoid any doubt, Numpy has been installed via pip and works fine. In addition, the two list objects are intentionally different shapes.

Here is my script/code:

    import numpy as np
    family_name = ['Homer','Marge','Maggie']
    family_age = [39,36,2,8]
    family_name_age = np.array([family_name, family_age], dtype=object)
    avg_family_age = family_name_age[1].mean()
       
    print('Average age: {:.2f}'.format(avg_family_age))

When I run this in a jupyter notebook, I get the following error message.

AttributeError

Traceback (most recent call last) c:\Users\my_pc\a_folder\01-numpy-practice.ipynb

in <cell line: 5>()

2 family_age = [39,36,2,8]

3 family_name_age= np.array([family_name, family_age], dtype=object)

----> 4 avg_family_age = family_name_age[1].mean()

6 print('Average age: {:.2f}'.format(avg_family_age))

AttributeError: 'list' object has no attribute 'mean'

However, when I try mean() as follows it works fine:

family_height_cm = [183,172,82]
family_weight_kg = [109,60,11]
family_bmi_data = np.array([family_height_cm,family_weight_kg])
avg_fam_height = family_bmi_data[0].mean()
print('Average height: {:.2f}'.format(avg_fam_height))

It works perfectly and I get a figure of Average height: 145.67

It would be very helpful if someone could give me some insight into what I'm doing wrong and an explanation of the theory that is as simple as possible. Mega thank you in advance.

CodePudding user response:

In the first example, you are specifying the dtype as object. This will leave you with an array of two python lists. You cannot call .mean() on a python list.

CodePudding user response:

family_name_age[1] is a list, but family_bmi_data[0] is a NumPy array. Unlike a NumPy array, a list does not have a mean method.

CodePudding user response:

@jprebys provided a clear reason why I encountered the error: "AttributeError: 'list' object has no attribute 'mean'"

In the first example, you are specifying the dtype as object. This will leave you with an array of two python lists. You cannot call .mean() on a python list - @jprebys https://stackoverflow.com/a/74116157/1753769

While @learner provided a code snippet that enabled me to achieve the result I was aiming for.

By first setting my variable: family_age as a NumPy array: family_age = np.array([39,36,2,8]). I was able to use the mean method/function as needed.

So my full code block as provided by @learner looked like this:

family_name = ['Homer','Marge','Maggie']
family_age = np.array([39,36,2,8])
family_name_age= np.array([family_name, family_age], dtype=object)
avg_family_age = family_name_age[1].mean()

print('Average age: {:.2f}'.format(avg_family_age))

Thank you @learner and @jprebys

  • Related