applying multiple summary statstics on a particular column of data frame

Viewed 23

I want to apply multiple statistics functions like mean, median, variance etc. on a particular column of a data frame. I used following code. It produced results but not in required manner. Please tell me how i can write such kind of functions.

def summary(x):
    output1=x["Close"].mean
    output2=x["Close"].median
    output=(output1,output2)
    return output
summary(infy)
required_output=(mean,median)
3 Answers
df.Close.agg(["mean", "median"]) 

Should do the trick

I think your code is missing braces. Apply this chunk of code.

def summary(x):
    output1=x["Close"].mean()
    output2=x["Close"].median()
    output=(output1,output2)
    return output
summary(infy)

It's because you need to call the mean and median methods:

x["Close"].mean()   # Note the brackets
x["Close"].median()

But as others have stated, pandas has a nice built-in agg for this task.

Related