Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
str.findall() method is also used to find substrings or separators in each string in a series. But it is different from str.find() method. Instead of returning the index, it returns list with substrings and size of list is number of times it occurred.
Syntax: Series.str.findall(pat, flags=0)
pat: Substring to be searched for
flags: Regex flags that can be passed (A, S, L, M, I, X), default is 0 which means None. For this regex module (re) has to be imported too.
Return Type: Series of list(strings).
To download the CSV used in code, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Example #1: Searching character in string
In this example, the name column is searched for ‘r’ using str.findall() method and output is stored in new column. Before doing any operations, null rows are dropped using .dropna() to avoid errors.
As shown in the output image, it can be compared that the number of ‘e’ returned is equal to number of time it occurred in string.
Example #2: Searching character and passing IGNORECASE flag
In this example, the Name column is searched for ‘a’ and the IGNORECASE flag is passed. For that re module has to be imported too. The returned series from str.findall() method is stored in a New column.
As shown in the output image, it can be seen in the first row itself that both ‘A’ and ‘a’ were returned since IGNORECASE flag(re.I) was passed.
- Python | pandas.to_markdown() in Pandas
- Python | pandas.map()
- Python | Pandas.apply()
- Python | Pandas TimedeltaIndex.contains
- Python | Pandas.Categorical()
- Python | Pandas DatetimeIndex.second
- Python | Pandas Series.get()
- Python | Pandas DatetimeIndex.day
- Python | Pandas.CategoricalDtype()
- Python | Pandas Series.mean()
- Python | Pandas dataframe.std()
- Python | Pandas TimedeltaIndex.name
- Python | Pandas dataframe.add()
- Python | Pandas dataframe.min()
- Python | Pandas series.str.get()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.