Selecting repeated identical char

Viewed 58

I am trying to spot sentences having identical repeated characters in the same word, for instance:

Sentence 
    are they saddddd?
    I don't want to go
    heyyyyy
    12333
    00unit
    00wolf                        
    01man                         
    20595                         
    2091996                       
    03dumbdumb                    

In order to assign a value 1 or 0 if they contains repeated characters or not (expected output below):

Sentence                         Lab
    are they saddddd?             1 
    I don't want to go            0
    heyyyyy                       1
    12333                         1
    00unit                        0 
    00wolf                        0
    01man                         0
    20595                         0
    2091996                       0
    03dumbdumb                    0

I have tried as follows:

df.loc[(df['Sentence'].str.findall(r'([a-zA-Z])\1{3}').astype(bool)), 'Lab']=1

But it does not assign correctly the value 1 whether at least 3 identical characters are consecutive.

Can you please have a look at my code and tell me why it is not working?

Some values, like 00unit, 00wolf, 01man, 20595, 2091996, 03dumbdumb, are wrongly selected from the code above, as including three identical consecutive characters, while they do not have them.

2 Answers

The pattern is any word character followed by at least two more of that character.

s = ['    are they saddddd?',
     "    I don't want to go",
     '    heyyyyy',
     '    12333',
     '    00unit',
     '    00wolf', 
     '    01man',
     '    20595',   
     '    2091996',   
     '    03dumbdumb']

df = pd.DataFrame(s,columns=['Sentence'])

In [25]: pattern = r'((\w)\2{2,})'

In [26]: df.loc[(df['Sentence'].str.findall(pattern).astype(bool)), 'Lab']=1

In [27]: df
Out[27]: 
                 Sentence  Lab
0       are they saddddd?  1.0
1      I don't want to go  NaN
2                 heyyyyy  1.0
3                   12333  1.0
4                  00unit  NaN
5                  00wolf  NaN
6                   01man  NaN
7                   20595  NaN
8                 2091996  NaN
9              03dumbdumb  NaN

Or pattern = r'(([a-zA-Z0-9])\2{2,})' if you don't want to match an underscore.


pattern = r'(([a-zA-Z0-9])\2{2,})'
S = df.Sentence.str.findall(pattern)
df['Lab'] = S.astype(bool).astype(int)

In [13]: df
Out[13]: 
                 Sentence  Lab
0       are they saddddd?    1
1      I don't want to go    0
2                 heyyyyy    1
3                   12333    1
4                  00unit    0
5                  00wolf    0
6                   01man    0
7                   20595    0
8                 2091996    0
9              03dumbdumb    0

How about putting \d in the character list, and change {3} to {2,}:

df['Lab'] = df['Sentence'].str.findall(r'([a-zA-Z\d])\1{2,}').astype(bool).astype(int)

Output:

             Sentence  Lab
0   are they saddddd?    1
1  I don't want to go    0
2             heyyyyy    1
3               12333    1
4              00unit    0
5              00wolf    0
6               01man    0
7               20595    0
8             2091996    0
9          03dumbdumb    0
Related