This is an extension to my question.
To make it simpler Lets suppose I have a pandas dataframe as following.
df = pd.DataFrame([[1.1, 1.1, 2.5, 2.6, 2.5, 3.4,2.6,2.6,3.4], list('AAABBBBAB'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3]]).T
df.columns = ['col1', 'col2','col3']
dataframe :
col1 col2 col3
0 1.1 A 1.1
1 1.1 A 1.7
2 2.5 A 2.5
3 2.6 B 2.6
4 2.5 B 3.3
5 3.4 B 3.8
6 2.6 B 4
7 2.6 A 4.2
8 3.4 B 4.3
I want to group this based on some conditions. The logic is based on col1 col2 values and the cumulative difference of col3:
- Go to col1 and find other occurrences of the same value.
- In my case first value of col1 is '1.1' and again their is the same value at row2.
- Then check for col2 value, If they are similar, then get the cumulative difference of col 3.
- If the cumulative difference is greater than 0.5 then mark this as a new session.
- If col1 values are same but col2 values are different then mark them as new session
expected output:
col1 col2 col3 session
0 1.1 A 1.1 0
1 1.1 A 1.7 1
2 2.5 A 2.5 2
3 2.6 B 2.6 4
4 2.5 B 3.3 3
5 3.4 B 3.8 7
6 2.6 B 4 5
7 2.6 A 4.2 6
8 3.4 B 4.3 7