Reputation: 641
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
Upvotes: 4
Views: 1696
Reputation: 863236
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
Upvotes: 3
Reputation: 367
make a new column with shift(1) and then separate by the difference between the values of these columns
Upvotes: 0