- 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.extract() 方法
Pandas 中的Series.str.extract() 方法允许您从 Series 中的每个字符串元素或 DataFrame 中的列中提取与指定正则表达式模式匹配的子字符串。此方法对于基于正则表达式模式将字符串的特定部分提取到 DataFrame 中的单独列中特别有用。
通过使用正则表达式模式,您可以轻松控制提取字符串的哪些部分。此方法从正则表达式模式的第一个匹配项中提取组。然后,提取的组可以作为 DataFrame 中的列返回,从而可以轻松处理复杂的文本数据。
语法
以下是 Pandas Series.str.extract() 方法的语法:
Series.str.extract(pat, flags=0, expand=True)
参数
Series.str.extract() 方法接受以下参数:
pat − 表示具有捕获组的正则表达式模式的字符串。
flags − 整数,默认为 0(无标志)。可以从re 模块(例如re.IGNORECASE)使用标志来修改正则表达式匹配。
expand − 布尔值,默认为 True。如果为 True,则返回一个 DataFrame,每个捕获组对应一列。如果为 False,则如果只有一个捕获组,则返回 Series/Index,如果有多个捕获组,则返回 DataFrame。
返回值
Series.str.extract() 方法返回 DataFrame、Series 或 Index。结果将对每个主题字符串有一行,对每个组有一列。正则表达式模式中的捕获组名称将用于列名;否则,将使用捕获组编号。即使找不到匹配项,每个结果列的 dtype 始终为 object。如果expand=False 并且模式只有一个捕获组,则返回 Series 或 Index。
示例 1
在此示例中,我们通过基于正则表达式模式提取 Series 中字符串的特定部分来演示 Series.str.extract() 方法的用法。
在这里,我们使用具有命名捕获组的正则表达式模式从 Series 中每个字符串中提取字母后跟数字。
import pandas as pd
import numpy as np
# Create a Series of strings
s = pd.Series(['Python1', 'Tutorialspoint2', 'caT3', np.nan])
# Extract letter followed by a digit
result = s.str.extract(r'(?P<letter>[a-zA-Z])(?P<digit>\d)')
print("Input Series:")
print(s)
print("\nDataFrame after calling str.extract():")
print(result)
运行上述代码后,将产生以下输出:
Input Series: 0 Python1 1 Tutorialspoint2 2 caT3 3 NaN dtype: object DataFrame after calling str.extract(): letter digit 0 n 1 1 t 2 2 T 3 3 NaN NaN
示例 2
在此示例中,我们演示了如何使用Series.str.extract() 方法基于具有多个捕获组的正则表达式模式提取 Series 中字符串的特定部分。
import pandas as pd
# Create a Series of strings
s = pd.Series(['Order123', 'Invoice456', 'Receipt789'])
# Extract 'Order', 'Invoice', or 'Receipt' followed by digits
result = s.str.extract(r'(?P<type>[A-Za-z]+)(?P<number>\d+)')
print("Input Series:")
print(s)
print("\nDataFrame after calling str.extract():")
print(result)
以下是上述代码的输出:
Input Series:
0 Order123
1 Invoice456
2 Receipt789
dtype: object
DataFrame after calling str.extract():
type number
0 Order 123
1 Invoice 456
2 Receipt 789
示例 3
在此示例中,我们演示了如何使用Series.str.extract() 方法从一系列电子邮件地址中提取电子邮件用户名。
import pandas as pd
# Create a Series of email addresses
s = pd.Series(['user1@example.com', 'info@tutorialspoint.com', 'contact@website.org'])
# Extract usernames from email addresses
result = s.str.extract(r'(?P<username>^[^@]+)')
print("Input Series:")
print(s)
print("\nDataFrame after calling str.extract()):")
print(result)
以下是上述代码的输出:
Input Series: 0 user1@example.com 1 info@tutorialspoint.com 2 contact@website.org dtype: object DataFrame after calling str.extract(): username 0 user1 1 info 2 contact