- 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 - 时间差
- Python Pandas - 稀疏数据结构
- Python Pandas - 稀疏数据
- Python Pandas - 可视化
- Python Pandas - 可视化
- Python Pandas - 其他概念
- Python Pandas - 警告和注意事项
- Python Pandas 有用资源
- Python Pandas - 快速指南
- Python Pandas - 有用资源
- Python Pandas - 讨论
Pandas Series.str.endswith() 方法
Pandas 中的Series.str.endswith()方法用于测试 Series 或索引中每个字符串元素的末尾是否与指定的模式匹配。此方法对于根据后缀模式过滤和分析文本数据很有用。
此方法类似于 Python 中的str.endswith()方法,提供了一种简单的方法来处理 Pandas Series 或索引中字符串末尾的模式匹配。
语法
以下是 Pandas Series.str.endswith()方法的语法:
Series.str.endswith(pat, na=None)
参数
Series.str.endswith()方法接受以下参数:
pat - 表示要测试每个元素末尾的字符序列的字符串或字符串元组。
na - 一个可选对象,用于显示被测试的元素是否不是字符串。默认值取决于数组的数据类型。对于对象数据类型,使用numpy.nan。对于StringDtype,使用pandas.NA。
返回值
Series.str.endswith()方法返回一个 Series 或索引,其中包含布尔值,指示给定模式是否与每个字符串元素的末尾匹配。
示例 1
在本例中,我们演示了Series.str.endswith()方法的基本用法,通过测试 Series 中的字符串是否以字符“t”结尾。
import pandas as pd
import numpy as np
# Create a Series of strings
s = pd.Series(['Python', 'Tutorialspoint', 'caT', np.nan])
# Test if strings end with 't'
result = s.str.endswith('t')
print("Input Series:")
print(s)
print("\nSeries after calling str.endswith('t'):")
print(result)
当我们运行上述代码时,它会产生以下输出:
Input Series:
0 Python
1 Tutorialspoint
2 caT
3 NaN
dtype: object
Series after calling str.endswith('t'):
0 False
1 True
2 False
3 NaN
dtype: object
示例 2
此示例演示了如何使用Series.str.endswith()方法来测试 Series 中的字符串是否以“n”或“T”结尾。
import pandas as pd
import numpy as np
# Create a Series of strings
s = pd.Series(['Python', 'Tutorialspoint', 'caT', np.nan])
# Test if strings end with 'n' or 'T'
result = s.str.endswith(('n', 'T'))
print("Input Series:")
print(s)
print("\nSeries after calling str.endswith(('n', 'T')):")
print(result)
以下是上述代码的输出:
Input Series:
0 Python
1 Tutorialspoint
2 caT
3 NaN
dtype: object
Series after calling str.endswith(('n', 'T')):
0 True
1 False
2 True
3 NaN
dtype: object
示例 3
Series.str.endswith()方法也可以应用于 DataFrame,以检查指定列中的每个元素是否以给定的字符串或字符结尾。该方法返回一个布尔 Series 对象。
import pandas as pd
# Creating a DataFrame for employees
employee_df = pd.DataFrame({
'Employee_ID': ['E101', 'E102', 'E103', 'E104', 'E105'],
'Name': ['John', 'Emily', 'Mark', 'Sarah', 'Jessica'],
'Department': ['Sales', 'HR', 'IT', 'Marketing', 'Finance'],
'Salary': [50000, 60000, 75000, 80000, 90000]
})
# Check if 'Name' column values end with 'h'
result = employee_df['Name'].str.endswith('h')
print("Printing Original Employee DataFrame:")
print(employee_df)
print("\nBoolean Series after calling str.endswith('h') on 'Name' column:")
print(result)
以下是上述代码的输出:
Printing Original Employee DataFrame:
Employee_ID Name Department Salary
0 E101 John Sales 50000
1 E102 Emily HR 60000
2 E103 Mark IT 75000
3 E104 Sarah Marketing 80000
4 E105 Jessica Finance 90000
Boolean Series after calling str.endswith('h') on 'Name' column:
0 True
1 False
2 False
3 True
4 False
Name: Name, dtype: bool
python_pandas_working_with_text_data.htm
广告