This is an extension for my question .
Make it easier. Suppose I have a pandas framework as shown below.
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 the values ββof col1 col2 and the cumulative difference col3:
- Go to col1 and find other occurrences of the same value.
- In my case, the first value of col1 is "1.1", and again their single value in line2.
- Then check the col2 value if they are similar, then get the cumulative difference in col 3.
- If the cumulative difference is greater than 0.5, then mark this as a new session.
- If the col1 values ββare the same, but the col2 values ββare different, then mark them as a 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
source share