- Python Pandas 教程
- Python Pandas - 首页
- Python Pandas - 简介
- Python Pandas - 环境设置
- Python Pandas - 基础
- Python Pandas - 数据结构简介
- Python Pandas - 索引对象
- Python Pandas - 面板
- 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 - 时间增量
- Python Pandas - 稀疏数据结构
- Python Pandas - 稀疏数据
- Python Pandas - 可视化
- Python Pandas - 可视化
- Python Pandas - 其他概念
- Python Pandas - 注意事项和陷阱
- Python Pandas 有用资源
- Python Pandas - 快速指南
- Python Pandas - 有用资源
- Python Pandas - 讨论
Python Pandas - 处理文本数据
Pandas 提供了使用.str访问器处理文本数据的强大工具。这允许我们在 Series 和 Index 对象上应用各种字符串操作,这些操作在 Pandas DataFrame 中有效地进行字符串操作。
.str访问器提供了各种字符串方法,可以对字符串对象执行字符串转换、连接、搜索等操作。下面,这些方法根据其功能进行分类:
字符串转换
此类别包括以某种方式转换字符串的方法,例如更改大小写、格式化或修改特定字符。
| 序号 | 方法和描述 |
|---|---|
| 1 |
将 Series 或 Index 中每个字符串的第一个字符转换为大写,其余转换为小写。 |
| 2 |
以更激进的方式将每个字符串转换为小写,适用于不区分大小写的比较。 |
| 3 |
将 Series 或 Index 中每个字符串的所有字符转换为小写。 |
| 4 |
Series.str.upper() 将 Series 或 Index 中每个字符串的所有字符转换为大写。 |
| 5 |
Series.str.title() 将每个字符串转换为标题大小写,其中每个单词的第一个字符都大写。 |
| 6 |
Series.str.swapcase() 交换大小写 - 将大写字符转换为小写,反之亦然。 |
| 7 |
Series.str.replace() 将每个字符串中模式或正则表达式的出现替换为另一个字符串。 |
字符串修剪
此类别包括用于将字符串修剪到特定字符或指定前缀的方法。
| 序号 | 方法和描述 |
|---|---|
| 1 |
Series.str.lstrip() 删除每个字符串开头的字符(默认为空格)。 |
| 2 |
Series.str.strip() 删除每个字符串开头和结尾的字符(默认为空格)。 |
| 3 |
Series.str.rstrip() 删除每个字符串结尾的字符(默认为空格)。 |
| 4 |
Series.str.removeprefix(prefix) 如果存在,则从 Series 或 Index 中每个字符串中删除指定的前缀。 |
| 5 |
Series.str.removesuffix(suffix) 如果存在,则从 Series 或 Index 中每个字符串中删除指定的尾缀。 |
字符串连接和联接方法
这些方法允许您将多个字符串组合成一个,或使用指定的分割符联接字符串内的元素。
| 序号 | 方法和描述 |
|---|---|
| 1 |
将 Series 或 Index 中的字符串与可选的分割符连接起来。 |
| 2 |
使用指定的分割符联接 Series 或 Index 中每个字符串包含的列表中的元素。 |
字符串填充方法
此类别包括用于将字符串填充到特定长度或在指定宽度内对其对齐的方法。
| 序号 | 方法和描述 |
|---|---|
| 1 |
将 Series 或 Index 中每个字符串在指定的宽度内居中,用字符填充。 |
| 2 |
Series.str.pad() 将 Series 或 Index 中每个字符串填充到指定的宽度,可以选择从左侧、右侧或两侧填充。 |
| 3 |
用指定的字符填充 Series 或 Index 中每个字符串的右侧,以达到指定的宽度。 |
| 4 |
Series.str.rjust() 用指定的字符填充 Series 或 Index 中每个字符串的左侧,以达到指定的宽度。 |
| 5 | Series.str.zfill() 用零填充 Series 或 Index 中每个字符串的左侧,直到达到指定的宽度。 |
字符串搜索方法
这些方法帮助您在文本中查找子字符串、计数出现次数或检查模式。
| 序号 | 方法和描述 |
|---|---|
| 1 |
检查 Series 或 Index 中每个字符串是否包含指定的模式。 |
| 2 |
计算 Series 或 Index 中每个字符串中模式或正则表达式的出现次数。 |
| 3 |
查找 Series 或 Index 中每个字符串中子字符串的最低索引。 |
| 4 |
Series.str.rfind() 查找 Series 或 Index 中每个字符串中子字符串的最高索引。 |
| 5 |
类似于 find(),但如果未找到子字符串则引发异常。 |
| 6 |
Series.str.rindex() 类似于 rfind(),但如果未找到子字符串则引发异常。 |
| 7 |
Series.str.match() 仅检查每个字符串开头的匹配项。 |
| 8 |
检查整个字符串的匹配项。 |
| 9 |
使用正则表达式提取每个字符串中匹配的组。 |
| 10 |
使用正则表达式提取每个字符串中的所有匹配项。 |
字符串分割方法
分割方法根据分隔符或模式划分字符串,这对于将文本数据解析成单独的组件很有用。
| 序号 | 方法和描述 |
|---|---|
| 1 |
Series.str.split() 按指定的分隔符或正则表达式分割 Series 或 Index 中的每个字符串,并返回字符串列表。 |
| 2 |
Series.str.rsplit() 按指定的分隔符或正则表达式分割 Series 或 Index 中的每个字符串,从右侧开始,并返回字符串列表。 |
| 3 |
Series.str.partition() 在每个字符串中分隔符的第一次出现处分割,并返回包含三个元素的元组:分隔符之前的部分、分隔符本身以及分隔符之后的部分。 |
| 4 |
Series.str.rpartition() 在每个字符串中分隔符的最后一次出现处分割,并返回包含三个元素的元组:分隔符之前的部分、分隔符本身以及分隔符之后的部分。 |
字符串过滤方法
这些方法对于过滤掉非字母数字字符、控制字符集或清理文本数据很有用。
| 序号 | 方法和描述 |
|---|---|
| 1 |
返回提供的函数计算结果为真的元素。 |
| 2 |
提取指定位置每个组件的元素。 |
| 3 |
Series.str.get_dummies() 按指定的分隔符分割 Series 中的每个字符串,并返回虚拟/指示变量的 DataFrame。 |
| 4 |
Series.str.isalpha() 检查每个字符串是否仅由字母字符组成。 |
| 5 |
Series.str.isdigit() 检查每个字符串是否仅由数字组成。 |
| 6 |
Series.str.isnumeric()s 检查每个字符串是否仅由数字字符组成。 |
| 7 |
Series.str.isspace() 检查每个字符串是否仅由空格组成。 |
| 8 |
Series.str.isupper() 检查每个字符串中的所有字符是否都大写。 |
| 9 |
Series.str.islower() 检查每个字符串中的所有字符是否都小写。 |
| 10 |
Series.str.isalnum() 检查每个字符串中的所有字符是否都为字母数字(字母和数字)。 |
| 11 | Series.str.istitle() 检查 Series 或 Index 中的每个字符串是否为标题大小写,其中每个单词都以大写字母开头。 |
| 12 |
Series.str.isdecimal() 检查每个字符串中的所有字符是否都为十进制字符。 |
| 13 |
计算 Series 或 Index 中每个字符串的长度。 |
| 14 |
查找每个字符串中模式或正则表达式的所有出现。 |
其他方法
此类别包括对字符串执行各种其他操作的方法,例如编码、解码和检查特定字符的存在。
| 序号 | 方法和描述 |
|---|---|
| 1 |
使用指定的编码对每个字符串进行编码。 |
| 2 |
使用指定的编码解码每个字符串。 |
| 3 |
将制表符字符('\t')扩展为空格。 |
| 4 |
将 Series 或 Index 中每个字符串重复指定的次数。 |
| 5 |
Series.str.slice_replace() 用传递的替换项替换每个字符串中的切片。 |
| 6 |
Series.str.translate() 通过转换表映射字符串中的每个字符。 |
| 7 |
Series.str.slice() 将 Series 或 Index 中的每个字符串根据传递的参数进行切片。 |
| 8 |
Series.str.startswith() 检查 Series 或 Index 中的每个字符串是否以指定的模式开头。 |
| 9 |
检查 Series 或 Index 中的每个字符串是否以指定的模式结尾。 |
| 10 |
Series.str.normalize() 将 Series 或 Index 中每个字符串的 Unicode 表示形式规范化为指定的规范化形式。 |
| 11 |
Series.str.wrap() 将 Series 或 Index 中的每个字符串换行到指定的行宽,根据需要换行。 |