I have a data frame that I would like to group by two parameters (1) consecutive numbering in the same first column and (2) matching values in the second column
data frame:
In [20]: import pandas as pd
In [21]: df1 = pd.DataFrame ({ "res": [30, 31, 35, 36], "ss": ["H", "H", "H", "E"], "AA": ["A", "B", "C", "D"]})
In [22]: df1
Out[22]:
res ss AA
0 30 H A
1 31 H B
2 35 H C
3 36 E D
Desired output:
group 1: (30, H, A), (31, H, B)
group 2: (35, H, C)
group 3: (36, E, D)
Group 1 includes the first 2 rows because 30 and 31 are consecutive and the second columns match. Group 2 is created because 31 and 35 in col 1 are not consecutive. Group 3 is created because H and E do not match.
I am trying to use groupby and enumerate together, but I can't seem to combine them.
Identify groups of continuous numbers in a list
grouping rows in list in pandas groupby
I appreciate any tips on how to combine the selections