

Python | Pandas Series.str.findall()
source link: https://www.geeksforgeeks.org/python-pandas-series-str-findall/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

- Last Updated : 28 Sep, 2018
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.
Pandas 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)
Parameters:
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.
# importing pandas module
import
pandas as pd
# making data frame
# removing null values to avoid errors
data.dropna(inplace
=
True
)
# string to be searched for
search
=
'r'
# returning values and creating column
data[
"Findall(name)"
]
=
data[
"Name"
].
str
.findall(search)
# display
data.head(
10
)
Output:
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.

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.
# importing pandas module
import
pandas as pd
# importing regex module
import
re
# making data frame
# removing null values to avoid errors
data.dropna(inplace
=
True
)
# string to be searched for
search
=
'a'
# returning values and creating column
data[
"Findall(name)"
]
=
data[
"Name"
].
str
.findall(search, flags
=
re.I)
# display
data.head(
10
)
Output:
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.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommend
-
9
QQA: Python 中的 str 与 repr有时候,你会需要为你的类实现 __str__ 或 __repr__ 方法,你知道它们的作用是什么吗?它们有什么区别吗?这个问题的答案一搜就能找到,如果恰巧这是你第一次看到这个问题,不妨看...
-
8
为自己定义每个python类添加__repr__和__str__发布于 10 分钟前在类定义里,使用__str__和__repr__双下划线能够自行控制类中的字符串转换...
-
8
ItsMyCode | One of the most common errors in Python programming is typeerror: ‘str’ object is not callable , and sometimes it will be painful to debug or find why this issue appeared in the first place. ...
-
3
Python爬虫编程思想(34):使用findall和finditer查找每一次出现的位置 ...
-
3
V2EX › Java 求助: JPA 使用 findAll 时执行了其他 SQL,该怎么排查 powinds · 3 小时 50 分...
-
5
Go 修改 struct 的打印信息,类似于 python __str__ Laurel-rao...
-
5
The Spring Data findAll Anti-Pattern Last modified: Sep 13, 2022 Imagine having a tool that can automatically detect JPA and Hibernate performance issues....
-
5
-
4
Python中str的+=性能分析 2022-11-20 19:34:25 +08 字数:1238 标签: Python
-
4
When Should You Use .__repr__() vs .__str__() in Python? Remove ads One of the most common...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK