简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM