w3resource

Pandas Series: str.extractall() function

Series-str.extractall() function

The str.extractall() function is used to extract groups from all matches of regular expression pat.

For each subject string in the Series, extract groups from all matches of regular expression pat. When each subject string in the Series has exactly one match, extractall(pat).xs(0, level=’match’) is the same as extract(pat).

Syntax:

Series.str.extractall(self, pat, flags=0)
Pandas Series: str.extractall() function

Parameters:

Name Description Type/Default Value Required / Optional
pat  Regular expression pattern with capturing groups. str Required
flags  A re module flag, for example re.IGNORECASE. These allow to modify regular expression matching for things like case, spaces, etc. Multiple flags can be combined with the bitwise OR operator, for example re.IGNORECASE | re.MULTILINE int
Default Value: 0 (no flags)
Required

Returns: A DataFrame with one row for each match, and one column for each group. Its rows have a MultiIndex with first levels that come from the subject Series. The last level is named 'match' and indexes the matches in each item of the Series. Any capture group names in regular expression pat will be used for column names, otherwise capture group numbers will be used.

Example - A pattern with one group will return a DataFrame with one column. Indices with no matches will not appear in the result:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["a2a3", "b2", "c2"], index=["X", "Y", "Z"])
s.str.extractall(r"[ab](\d)")

Output:

        0
  match	
X	 0	  2
1	 3
Y	 0	  2

Example - Capture group names are used for column names of the result:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["a2a3", "b2", "c2"], index=["X", "Y", "Z"])
s.str.extractall(r"[ac](?P<digit>\d)")

Output:

        digit
  match	
X	  0	     2
    1	     3
Z	  0	     2
Pandas Series: str.extractall() function

Example - A pattern with two groups will return a DataFrame with two columns:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["a2a3", "b2", "c2"], index=["X", "Y", "Z"])
s.str.extractall(r"(?P<letter>[ac])(?P<digit>\d)")

Output:

         letter	digit
  match		
X	 0	    a	    2
1	        a	    3
Z	 0	    c	    2

Example - Optional groups that do not match are NaN in the result:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["a2a3", "b2", "c2"], index=["X", "Y", "Z"])
s.str.extractall(r"(?P<letter>[ac])?(?P<digit>\d)")

Output:

          letter	digit
  match		
X	 0	       a	2
   1         a	3
Y	 0	       NaN	2
Z	 0	       c	2

Previous: Series-str.extract() function
Next: Series-str.findall() function



Follow us on Facebook and Twitter for latest update.