-1

I have an example dataframe as shown below

   x    y   dx
0  1  6.0  1.1
1  2  6.0  1.5
2  2  6.5  1.2
3  3  7.2  4.3
4  4  7.5  4.5
5  4  8.0  4.7
6  5  1.1  7.0

I would like to merge the rows if the values in column dx are within a range of 1 of each other. There will be no overlapping ranges. I can either keep one of those rows and drop the rest or take an average of all the rows. So the expected output would look like

   x    y   dx
1  1  6.0  1.1
2  3  7.2  4.3
3  5  1.1  7.0

or

   x     y     dx
0  1.67  6.17  1.26
1  3.67  7.57   4.5
2  5     1.1   7.0
beeprogrammer
  • 581
  • 1
  • 7
  • 18

2 Answers2

1

You can have the first option with the following:

import pandas as pd
new_df=df[0:1]
for i in range(1,len(df)):
    if df.dx.iloc[i]-new_df.dx.iloc[-1]>1:
        new_df=pd.concat([new_df, df.iloc[i:i+1,:]], ignore_index=True)
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
0

Try this

df_final = df.groupby((df.dx.diff().abs() > 1).cumsum(), as_index=False).first()

Out[288]:
   x    y   dx
0  1  6.0  1.1
1  3  7.2  4.3
2  5  1.1  7.0
Andy L.
  • 24,909
  • 4
  • 17
  • 29