I have the following dataframe:
     x     text     
1    500   aa
2    550   bb
3    700   cc
4    750   dd 
My goal is to split this df if the x-values are more than 100 points apart.
Is there a pandas function that allows you to make a split based on range of values?
Here is my desired output:
df_1:
    x     text  
0   500   aa
1   550   bb
df_2:
    x     text  
0   700   cc
1   750   dd
                I believe you need convert groupby object to tuple and dictionary by helper Series:
d = dict(tuple(df.groupby(df['x'].diff().gt(100).cumsum())))
print (d)
{0:      x text
1  500   aa
2  550   bb, 1:      x text
3  700   cc
4  750   dd}
Detail:
First get difference by Series.diff, compare by Series.gt for greater and create consecutive groups by Series.cumsum:
print (df['x'].diff().gt(100).cumsum())
1    0
2    0
3    1
4    1
Name: x, dtype: int32
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With