Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between `Series.str.contains("|")` and `Series.apply(lambda x:"|" in x)` in pandas?

Tags:

python

pandas

This is the code for testing:

import numpy as np # maybe you should download the package
import pandas as pd # maybe you should download the package
data = ['Romance|Fantasy|Family|Drama', 'War|Adventure|Science Fiction',
       'Action|Family|Science Fiction|Adventure|Mystery', 'Action|Drama',
       'Action|Drama|Thriller', 'Drama|Romance', 'Comedy|Drama', 'Action',
       'Comedy', 'Crime|Comedy|Action|Adventure',
       'Drama|Thriller|History', 'Action|Science Fiction|Thriller']

a = pd.Series(data)
print(a.str.contains("|"))
print(a.apply(lambda x:"|" in x))
print(a)

After executing the code above, you will get this three output:

0     True
1     True
2     True
3     True
4     True
5     True
6     True
7     True
8     True
9     True
10    True
11    True
dtype: bool

print(a.apply(lambda x:"|" in x)) output is:

0      True
1      True
2      True
3      True
4      True
5      True
6      True
7     False
8     False
9      True
10     True
11     True
dtype: bool

print(a) output is:

image.png

You will see in 7 and 8 in Series a do not have |. However the return of print(a.str.contains("|")) is all True. What is wrong here?

like image 903
Rachel Jennifer Avatar asked Jun 21 '18 16:06

Rachel Jennifer


1 Answers

| has a special meaning in RegEx, so you need to escape it:

In [2]: a.str.contains(r"\|")
Out[2]:
0      True
1      True
2      True
3      True
4      True
5      True
6      True
7     False
8     False
9      True
10     True
11     True
dtype: bool
like image 125
MaxU - stop WAR against UA Avatar answered Sep 28 '22 01:09

MaxU - stop WAR against UA