simar
simar

Reputation: 605

applying multiple summary statstics on a particular column of data frame

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)

Upvotes: 2

Views: 36

Answers (3)

SimonR
SimonR

Reputation: 1824

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.

Upvotes: 0

jeetkamal
jeetkamal

Reputation: 409

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)

Upvotes: 1

Igor Rivin
Igor Rivin

Reputation: 4864

df.Close.agg(["mean", "median"]) 

Should do the trick

Upvotes: 2

Related Questions