s = pd.DataFrame(["I love you", "Pandas", "<3"], index=["A", "B", "C"],columns=['a'])
io=r"\b(Pandas)\b"
s['a'].str.extractall(io)
Expected Output
0
match
B 0 Pandas
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.2.0-27-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8
pandas: 0.18.1 nose: 1.3.7 pip: 8.1.2 setuptools: 27.2.0 Cython: 0.24.1 numpy: 1.11.1 scipy: 0.18.1 statsmodels: 0.6.1 xarray: None IPython: 5.1.0 sphinx: 1.4.6 patsy: 0.4.1 dateutil: 2.5.3 pytz: 2016.6.1 blosc: None bottleneck: 1.1.0 tables: 3.2.3.1 numexpr: 2.6.1 matplotlib: 1.5.3 openpyxl: 2.3.2 xlrd: 1.0.0 xlwt: 1.1.2 xlsxwriter: 0.9.3 lxml: 3.6.4 bs4: 4.5.1 html5lib: 0.999999999 httplib2: None apiclient: None sqlalchemy: 1.0.13 pymysql: None psycopg2: None jinja2: 2.8 boto: 2.42.0 pandas_datareader: None
I am new to extractall() and the google resources are somewhat scarce. I am trying to find the rows with the most hits of the words in a string so I am using normal regex syntax to construct a pattern to look for all whole words. It works fine if I look for more than one word:
io=r"\b(I)\b|\b(love)\b|\b(you)\b" s['a'].str.extractall(io) 0 1 2 match
A 0 I NaN NaN 1 NaN love NaN 2 NaN NaN you
But for one word it fails, if use just extract then it works.
io=r"\b(Pandas)\b" s['a'].str.extract(io) A NaN B Pandas C NaN
I then obviously have to change my logic based on if it is a single or multi word.
Unless I add a bogus lookup!
io=r"\b(Pandas)\b|\b(blahblahblah)\b" s['a'].str.extractall(io) 0 1 match
B 0 Pandas NaN
Am I doing something daft? Is this a bug or maybe a feature if so could this be changed to work on a single lookup?
Thanks
Comment From: jreback
This looks correct. You might want to try .extract
. .extractall
gets multiple matches rather than just the first one. docs are pretty extensive
In [13]: s['a'].str.extract(io,expand=False)
Out[13]:
A NaN
B Pandas
C NaN
Name: a, dtype: object
In [14]: s['a'].str.extract(io,expand=True)
Out[14]:
0
A NaN
B Pandas
C NaN
In [15]: s['a'].str.extractall(io)
Out[15]:
0
match
B 0 Pandas