Home > Back-end >  How to check strings for special symbols
How to check strings for special symbols

Time:10-27

I have to check each row of the table for special symbols and add a column with the number of symbols without using cycles,but i have to use regular expressions

I write a regular expression:

rexp = '("|#|%|\[|\])'
len(re.findall(rexp, 'asdfasf[sadfsaf%sadfad]]'))

And I try to solve it like this:

def special_symbols(x):
    if len(re.findall(rexp, x['game_description'])) >0:
        return len(re.findall(rexp, x['game_description']))
    else:
        return 0

games_df['n_special_symbols'] = games_df.apply(special_symbols, axis =1)

But I have an error:

expected string or bytes-like object

How can I fix it?

CodePudding user response:

games_df['n_special_symbols'] = games_df['your column with the string goes here'].apply(special_symbols)

CodePudding user response:

There's no need to use apply. It's much faster and easier to address a built-in pandas.Series.str.count which takes a regular expression as a parameter:

df = pd.DataFrame({'game_description':['one[two]three','four#five','%six%seven%','eight']})
special = '"|#|%|\[|\]'
df['n_special_symbols'] = df['game_description'].str.count(special)

Output:

  game_description  n_special_symbols
0    one[two]three                  2
1        four#five                  1
2      %six%seven%                  3
3            eight                  0
  • Related