How can I find the beginning and end of a character in Python

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))]  # [20]
lc2 = df.index[df.Col.eq('X') & df.Col.ne(df.Col.shift(-1))]  # [14,29]

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?

+4
1

A, B C, :

df[(df.Col =='A')|(df.Col =='B')|(df.Col =='C')].index

:

df1 = df[df['count'] != df['count'].shift(+1)]
print df1[df1['count'] != 0]['count']

:

df2 = df[df['count'] != df['count'].shift(-1)]
print df2[df2['count'] != 0]['count']

count , count DataFrame, df.count.

EDIT: , .

+1

Source: https://habr.com/ru/post/1653444/


All Articles