Laura
Laura

Reputation: 1282

Pandas DataFrame Conditional Groupby

I have this DF:

df = pd.DataFrame(data=[[-2.000000, -1.958010,  0.2],
                   [-1.958010, -1.916030,  0.4],
                   [-1.916030, -1.874040,  0.3],  
                   [-1.874040, -1.832050,  0.6],
                   [-1.832050, -1.790070, 0.8],
                   [-1.790070, -1.748080,  0.2]],columns=['egystart','egyend','fx'])

So I want to groupby every two rows and get fx as the mean value of the two rows. egystart should by egystart of the first row and egyend should by egyend of the second row.

In this case I should obtain:

-2.000000 -1.916030  0.3
-1.916030 -1.832050  0.45  
-1.832050 -1.748080  0.5

So I have tried something like this:

df.groupby((df.egystart == df.egyend.shift(1)).cumsum()).agg({'egystart':min, 'egyend':max, 'fx':HERE_THE_MEAN_VALUE})

But it doesnt work

Upvotes: 0

Views: 71

Answers (1)

Sam Comber
Sam Comber

Reputation: 1293

You could try this to get the mean of fx every 2 rows:

result = df.groupby(np.arange(len(df))//2).mean()

print(result)

   egystart    egyend    fx
0 -1.979005 -1.937020  0.30
1 -1.895035 -1.853045  0.45
2 -1.811060 -1.769075  0.50

Upvotes: 1

Related Questions