I have a dataframe with two columns - Col1 and Col2. I want to iterate through it and return only the values that follow the pattern:
if A followed by B, return AB, else return nothing. I want to basically get rid of all A's that are not immediatelly followed by B's. Note that B's are in my real example always preceded by A.
df = pd.DataFrame({"Col1": ['A', 'B', 'A', 'A', 'B'],
"Col2": [11, 22, 33, 44, 55]})
Col1 Col2
0 A 11
1 B 22
2 A 33
3 A 44
4 B 55
I came up with this code:
for index, row in df.iterrows():
if row['Col1'] == 'A':
if row.shift(1)['Col1'] == 'B':
print(row)
print(row.shift(1))
else:
continue
else:
continue
I did just the "print function" for now to see if the correct entry prints but I'll be glad if anyone helps me with a way to create a new DF with these rows.
The code above returns nothing, but also no error.
Edit: example desired output
Col1 Col2
0 A 11
1 B 22
3 A 44
4 B 55