I'm stuck in a problem, because I can't find any solution to deal with it, I have the following sample:
data = [['John', 6, 'A'], ['Paul', 6, 'D'],
['Juli', 9, 'D'], ['Geeta', 4, 'A'],
['Jay', 6, 'D'], ['Sara', 6, 'A'],
['Mario', 3, 'D'], ['Peter', 6, 'A'],
['Jin', 6, 'D'], ['Carl', 6, 'A']]
df = pd.DataFrame(data, columns=['Name', 'Number', 'Label'])
I previously grouped by number with the following line of code:
df = df.sort_values('number')
and got this output:
Name Number Label
Mario 3 D
Geeta 4 A
Peter 4 A
Jin 4 D
John 6 A
Paul 6 D
Jay 6 D
Sara 6 A
Carl 6 A
Juli 9 D
So I just want to select pair of rows which have an 'A' in the last column and followed by a row with a 'D' in the last column, and find all pair of rows that match this pattern in the same group (I don't want the last 'A' of a group and the 'D' of the next group), so the solution of the problem is:
Name Number Label
Peter 4 A
Jin 4 D
John 6 A
Paul 6 D
Anyone can help me?