- Python Pandas 教程
- Python Pandas - 首页
- Python Pandas - 简介
- Python Pandas - 环境搭建
- Python Pandas - 基础
- Python Pandas - 数据结构介绍
- Python Pandas - 索引对象
- Python Pandas - Panel (面板)
- Python Pandas - 基本功能
- Python Pandas - 索引和数据选择
- Python Pandas - Series (序列)
- Python Pandas - Series (序列)
- Python Pandas - Series 对象切片
- Python Pandas - Series 对象的属性
- Python Pandas - Series 对象的算术运算
- Python Pandas - 将 Series 转换为其他对象
- Python Pandas - DataFrame (数据框)
- Python Pandas - DataFrame (数据框)
- Python Pandas - 访问 DataFrame
- Python Pandas - DataFrame 对象切片
- Python Pandas - 修改 DataFrame
- Python Pandas - 从 DataFrame 中删除行
- Python Pandas - DataFrame 的算术运算
- Python Pandas - I/O 工具
- Python Pandas - I/O 工具
- Python Pandas - 使用 CSV 格式
- Python Pandas - 读取和写入 JSON 文件
- Python Pandas - 从 Excel 文件读取数据
- Python Pandas - 将数据写入 Excel 文件
- Python Pandas - 使用 HTML 数据
- Python Pandas - 剪贴板
- Python Pandas - 使用 HDF5 格式
- Python Pandas - 与 SQL 的比较
- Python Pandas - 数据处理
- Python Pandas - 排序
- Python Pandas - 重新索引
- Python Pandas - 迭代
- Python Pandas - 连接
- Python Pandas - 统计函数
- Python Pandas - 描述性统计
- Python Pandas - 使用文本数据
- Python Pandas - 函数应用
- Python Pandas - 选项和自定义
- Python Pandas - 窗口函数
- Python Pandas - 聚合
- Python Pandas - 合并/连接
- Python Pandas - 多层索引
- Python Pandas - 多层索引基础
- Python Pandas - 使用多层索引进行索引
- Python Pandas - 使用多层索引进行高级重新索引
- Python Pandas - 重命名多层索引标签
- Python Pandas - 对多层索引进行排序
- Python Pandas - 二元运算
- Python Pandas - 二元比较运算
- Python Pandas - 布尔索引
- Python Pandas - 布尔掩码
- Python Pandas - 数据重塑和透视
- Python Pandas - 透视表
- Python Pandas - 堆叠和取消堆叠
- Python Pandas - 熔化
- Python Pandas - 计算虚拟变量
- Python Pandas - 分类数据
- Python Pandas - 分类数据
- Python Pandas - 分类数据的排序和排序
- Python Pandas - 分类数据的比较
- Python Pandas - 处理缺失数据
- Python Pandas - 缺失数据
- Python Pandas - 填充缺失数据
- Python Pandas - 缺失值的插值
- Python Pandas - 删除缺失数据
- Python Pandas - 使用缺失数据进行计算
- Python Pandas - 处理重复项
- Python Pandas - 重复数据
- Python Pandas - 计数和检索唯一元素
- Python Pandas - 重复标签
- Python Pandas - 分组和聚合
- Python Pandas - GroupBy
- Python Pandas - 时间序列数据
- Python Pandas - 日期功能
- Python Pandas - Timedelta (时间差)
- Python Pandas - 稀疏数据结构
- Python Pandas - 稀疏数据
- Python Pandas - 可视化
- Python Pandas - 可视化
- Python Pandas - 附加概念
- Python Pandas - 警告和陷阱
- Python Pandas 有用资源
- Python Pandas - 快速指南
- Python Pandas - 有用资源
- Python Pandas - 讨论
Pandas Series.str.findall() 方法
Python Pandas 中的Series.str.findall()方法用于查找 Series 或 Index 中每个字符串中模式或正则表达式的所有出现。此方法等效于将re.findall()应用于 Series/Index 中的所有元素。
该方法返回一个 Series 或 Index 的列表,其中每个列表包含在相应字符串中找到的模式或正则表达式的所有不重叠匹配。它对于查找和提取 Pandas Series、Index 或 DataFrame 列中每个字符串中指定模式或正则表达式的所有不重叠出现非常有用。
语法
以下是 Pandas Series.str.findall() 方法的语法:
Series.str.findall(pat, flags=0)
参数
Series.str.findall() 方法接受以下参数:
pat - 表示要搜索的模式或正则表达式的字符串。
flags - 可选整数,默认为 0。来自 re 模块的标志,例如 re.IGNORECASE,用于修改模式匹配行为。
返回值
Series.str.findall() 方法返回一个包含字符串列表的 Series 或 Index。每个列表包含在相应字符串中找到的模式或正则表达式的所有不重叠匹配。如果未找到匹配项,则为这些元素返回空列表。
示例 1
此示例演示如何在 Series 中每个字符串元素中查找子字符串“t”的所有出现。
import pandas as pd
# Create a Series of strings
s = pd.Series(['tutorials', 'articles', 'Examples'])
# Find all occurrences of the substring 't' in each string
result = s.str.findall('t')
print("Input Series:")
print(s)
print("\nOccurrences of 't':")
print(result)
运行以上代码后,将产生以下输出:
Input Series: 0 tutorials 1 articles 2 Examples dtype: object Occurrences of 't': 0 [t, t] 1 [t] 2 [] dtype: object
空列表[]表示元素中没有模式出现。
示例 2
此示例演示如何使用正则表达式查找所有模式的出现。在这里,我们查找所有以“t”开头后跟任何字符的子字符串。
import pandas as pd
# Create a Series of strings
s = pd.Series(['tutorials', 'testing', 'test cases'])
# Find all substrings starting with 't' followed by any character
result = s.str.findall(r't.')
print("Input Series:")
print(s)
print("\nOccurrences of pattern 't.':")
print(result)
运行以上代码后,将产生以下输出:
Input Series: 0 tutorials 1 testing 2 test cases dtype: object Occurrences of pattern 't.': 0 [tu, to] 1 [te, ti] 2 [te, t ] dtype: object
输出显示正则表达式模式“t.”的匹配列表,其中每个元素代表与模式匹配的子字符串。
示例 3
此示例演示将Series.str.findall()方法应用于 DataFrame。我们在 DataFrame 中查找与指定模式匹配的所有电子邮件地址。
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({
'Email': ['user1@example.com', 'info@tutorialspoint.com', 'contact@website.org']
})
# Find all occurrences of the pattern 'tutorialspoint.com' in the 'Email' column
result = df['Email'].str.findall('tutorialspoint.com')
print("Input DataFrame:")
print(df)
print("\nOccurrences of 'tutorialspoint.com':")
print(result)
运行以上代码后,将产生以下输出:
Input DataFrame:
Email
0 user1@example.com
1 info@tutorialspoint.com
2 contact@website.org
Occurrences of 'tutorialspoint.com':
0 []
1 [tutorialspoint.com]
2 []
Name: Email, dtype: object
输出显示仅在第二个电子邮件地址中找到了模式“tutorialspoint.com”。