I have a dataframe dfwith the following identifiers (in Col). The last occurrence of A / B / C represents the beginning, and the last occurrence of X is the end. I should ignore any other A, B, C between the beginning and the end (e.g. lines 8 and 9).
I have to find the start and end entries from this data and assign a number to each of these cases. The column countis my desired result:
Col ID
P
Q
A
A
A 1
Q 1
Q 1
B 1
C 1
S 1
S 1
X 1
X 1
X 1
Q
Q
R
R
C
C 2
D 2
E 2
B 2
K 2
D 2
E 2
E 2
X 2
X 2
This code:
lc1 = df.index[df.Col.eq('A') & df.Col.ne(df.Col.shift(-1))]
will provide me with an array of all the latest index values "A", in this case [5].
lc1 = df.index[df.Col.eq('C') & df.Col.ne(df.Col.shift(-1))]
lc2 = df.index[df.Col.eq('X') & df.Col.ne(df.Col.shift(-1))]
I would use ilocto print counter values:
df.iloc[5:14]['count'] = 1
df.iloc[20:29]['count'] = 2
How can I find A / B / C indices together and print count values for each beginning and end of an occurrence?