Pandas Series: str.findall() function
Series-str.findall() function
The str.findall() function is used to Find all occurrences of pattern or regular expression in the Series/Index.
Equivalent to applying re.findall() to all the elements in the Series/Index.
Syntax:
Series.str.findall(self, pat, flags=0, **kwargs)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
pat | Pattern or regular expression. | str | Required |
flags | Flags from re module, e.g. re.IGNORECASE (default is 0, which means no flags). | int Default Value: 0 |
Required |
Returns: Series/Index of lists of strings
All non-overlapping matches of pattern or regular expression in each string of this Series/Index.
Example - The search for the pattern ‘Deer’ returns one match:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
s.str.findall('Deer')
Output:
0 [] 1 [Deer] 2 [] dtype: object
Example - On the other hand, the search for the pattern ‘DEER’ doesn’t return any match:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
s.str.findall('DEER')
Output:
0 [] 1 [] 2 [] dtype: object
Example - Flags can be added to the pattern or regular expression. For instance, to find the pattern ‘DEER’ ignoring the case:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
import re
s.str.findall('DEER', flags=re.IGNORECASE)
Output:
0 [] 1 [Deer] 2 [] dtype: object
Example - When the pattern matches more than one string in the Series, all matches are returned:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
s.str.findall('er')
Output:
0 [er] 1 [er] 2 [] dtype: object
Example - Regular expressions are supported too. For instance, the search for all the strings ending with the word ‘er’ is shown next:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
s.str.findall('er$')
Output:
0 [er] 1 [er] 2 [] dtype: object
Example - If the pattern is found more than once in the same string, then a list of multiple strings is returned:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Tiger', 'Deer', 'Zoo'])
s.str.findall('o')
Output:
0 [] 1 [] 2 [o, o] dtype: object
Previous: Series-str.extractall() function
Next: Series-str.get() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-str-findall.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics