I have a data frame as follow:-
JavaScript
x
7
1
df=
2
a b
3
goat* bat
4
ki^ck ball
5
range@ kick
6
rick? kill
7
Now I want to find the count of total special characters present in each column. So I have used str. contains
function to find it, though it is running but it does not find the special characters.
JavaScript
1
7
1
code:-
2
special = df.filter(df['a'].contains('[!@$^&-_;:?.#*]'))
3
print(special.count())
4
5
output:- 0
6
excepted output:- 4
7
Advertisement
Answer
You may want to use rlike
instead of contains
, which allows to search for regular expressions
JavaScript
1
3
1
df.filter(df['a'].rlike('[!@$^&-_;:?.#*]')).count()
2
# 4
3