- 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 - 多级索引 (MultiIndex)
- 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.find() 方法
Pandas 中的 Series.str.find() 方法用于返回 Series 中每个字符串中找到指定子字符串的最低索引。如果未找到子字符串,则返回 -1。
此方法等效于标准 Python str.find() 方法。它可用于在 Pandas Series 或 DataFrame 的列中的字符串内查找子字符串,并通过可选的开始和结束参数提供灵活性。
语法
以下是 Pandas Series.str.find() 方法的语法:
Series.str.find(sub, start=0, end=None)
参数
Series.str.find() 方法接受以下参数:
sub - 表示要搜索的子字符串的字符串。
start - 可选整数,默认为 0。它表示搜索开始的左边缘索引。
end - 可选整数,默认为 None。它表示执行搜索的右边缘索引。
返回值
Series.str.find() 方法返回一个 Series 或索引,其中包含表示找到子字符串的最低索引的整数。如果未找到子字符串,则对于这些元素返回 -1。
示例 1
此示例演示了如何使用 Series.str.find() 方法在 Series 中每个字符串元素中查找子字符串的最低索引。
import pandas as pd
# Create a Series of strings
s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])
# Find the index of the substring 'e' in each string
result = s.str.find('e')
print("Input Series:")
print(s)
print("\nIndexes of Substring 'e':")
print(result)
当我们运行以上代码时,它会产生以下输出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 'e': 0 -1 1 -1 2 6 3 6 dtype: int64
示例 2
此示例演示了如何在 Series 中每个字符串元素的指定范围内查找子字符串的最低索引。
import pandas as pd
# Create a Series of strings
s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])
# Find the index of the substring 't' within the range [2:10] in each string
result = s.str.find('t', start=2, end=10)
print("Input Series:")
print(s)
print("\nIndexes of Substring 't' within [2:10]:")
print(result)
当我们运行以上代码时,它会产生以下输出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 't' within [2:10]: 0 2 1 3 2 2 3 -1 dtype: int64
指定的范围 [2:10] 用于限制在每个字符串元素中搜索子字符串 't'。
示例 3
此示例演示了当子字符串不在某些元素中时,如何在 Series 中每个字符串元素中查找子字符串的最低索引。
import pandas as pd
# Create a Series of strings
s = pd.Series(['python', 'Tutorialspoint', 'articles', 'Examples'])
# Find the index of the substring 'z' in each string
result = s.str.find('z')
print("Input Series:")
print(s)
print("\nIndexes of Substring 'z':")
print(result)
当我们运行以上代码时,它会产生以下输出:
Input Series: 0 python 1 Tutorialspoint 2 articles 3 Examples dtype: object Indexes of Substring 'z': 0 -1 1 -1 2 -1 3 -1 dtype: int64
所有元素的 -1 值表示子字符串 'z' 在任何字符串元素中都不存在。
python_pandas_working_with_text_data.htm
广告