Reputation: 309
I've got dataframe with below structure:
Id. Value.
1. 10
2. 12
3. 14
4. 16
5. 30
6. 32
7. 35
8. 36
9. 60
10. 61
11. 63
12. 67
13. 70
And I want to split it into number of groups using a conditional statement, which is comparing subsequent rows in my dataframe, What is the best way to do it?
Upvotes: 0
Views: 244
Reputation: 153500
IIUC, you try could this:
s = pd.Series([10,12,14,16,30,32,35,36,60,61,63,67,70])
s.groupby((s.diff() >= 10).cumsum()).apply(list)
Output:
0 [10, 12, 14, 16]
1 [30, 32, 35, 36]
2 [60, 61, 63, 67, 70]
dtype: object
For a dataframe:
df = pd.DataFrame({'id':np.arange(1,14),
'Value':[10,12,14,16,30,32,35,36,60,61,63,67,70]})
df.groupby(df['Value'].diff().ge(10).cumsum())['Value'].apply(list).tolist()
Output:
[[10, 12, 14, 16], [30, 32, 35, 36], [60, 61, 63, 67, 70]]
Upvotes: 1