Pandas groupby custom function to each series

Viewed 26224

I am having hard time to apply a custom function to each set of groupby column in Pandas

My custom function takes series of numbers and takes the difference of consecutive pairs and returns the mean of all the differences. Below is the code

def mean_gap(a):
    b = []
    for i in range(0, len(a)-1):
        b.append((a[i+1]-a[i]))
    return np.mean(b)

so if a = [1,3,7] mean_gap(a) will give me ((3-1)+(7-3))/2) = 3.0

 Dataframe:
   one two
    a  1
    a  3
    a  7
    b  8
    b  9

desired result
     Dataframe:
       one two
        a  3
        b  1

df.groupby(['one'])['two'].???

I am new to pandas. I read that groupby takes values each row at a time, not full series. So I am not able to use lambda after groupby. Please help!

1 Answers
Related