Change language

Python | Pandas Series.str.findall ()

| |

str.findall() Pandas str.findall() is also used to find substrings or delimiters in each line of the series. But this is different from the

Example # 1: Search for a character in a string

This example searches the name column for & # 39; r & # 39; using the str.findall () method and the output is saved in a new column. Dropna () deletes null lines before performing any operations to avoid errors.

# pandas module import

import pandas as pd 

  
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

  
# remove null values ​​to avoid errors

data.dropna (inplace = True

  
# search string

search = ’ r’

 
# return values ​​and create a column

data [ "Findall (name)" ] = data [ "Name" ]. str . Findall (search)

 
# display

data.head ( 10 )

Logout:
As shown in the output image, you can compare that the number of "e" returned is equal to the number of times it was encountered in the string. 

Example # 2: Search for a character and send the IGNORECASE flag

This example searches the Name column for "a" and passes the IGNORECASE flag. For this, the re module must also be imported. The returned row from the str.findall () method is stored in the New column.

# pandas module import

import pandas as pd 

 
# regex module import

import re

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/ nba.csv "

   # remove null values ​​to avoid errors

data.dropna (inplace = True

  
# search string

search = ’ a’

 
# return values ​​and create a column

data [ "Findall (name)" ] = data [ " Name " ]. str . Findall (search, flags = re.I)

 
# display

data.head ( 10 )

Exit:
As shown in the output image, the very first line shows that both "A" and "a" were returned after the IGNORECASE flag (re.I) was passed.