Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

calculationg the mean from each Dataframe column

I’ve to write a function (column_means), that calculates the mean of each column from Dataframe and give me a list of means at the end. I’m not allowed to use the mean function .mean(), so I’m implementing the general formula of the mean: sum(x_i)/Number of elements.

This is my code:

df = pd.DataFrame({'a':[1,2,3], 'b': [4,5,6]})
def column_means(df):
    means  = [] 
    for i,n in zip(df.columns,  df.shape[0]):
        means [n] = sum(df[i])/ df.shape[0]
    return means

It doesn’t work as intended. could you please help me and tell me, what are my mistakes?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance.

>Solution :

You are iterating over int in zip function, as df.shape[0] is returning single integer and not an iterable datatype.

So you can simply do as following:

def column_means(df):
    means = []
    for i in df.columns:
        means.append(sum(df[i]) / df.shape[0])
    return means

And if you want mean to be just an integer instead of float, you can just do sum(df[i]) // df.shape[0]

I hope this answers your question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading