Python Pandas - 指示除最后一次出现之外的重复索引值
pythonpandasserver side programmingprogramming更新于 2025/12/31 9:16:17
要指示除最后一次出现之外的重复索引值,请使用 index.duplicated()。使用 keep 参数,值为 last。
首先,导入所需的库 −
import pandas as pd
创建带有一些重复项的索引−
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
显示索引 −
print("Pandas Index with duplicates...\n",index)
将重复的索引值指示为 True,但最后一次出现的情况除外。将"keep" 参数设置为"last" −
print("\nIndicating duplicate values except the last indication...\n", index.duplicated(keep='last'))
示例
以下是代码 −
import pandas as pd
# 创建包含一些重复项的索引
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
# 显示索引
print("Pandas Index with duplicates...\n",index)
# 返回索引的 dtype数据
print("\ndtype 对象...\n",index.dtype)
# 获取数据的维度
print("\n获取维度...\n",index.ndim)
# 将重复的索引值指示为 True,最后一个值除外
# 将"keep" 参数设置为"last"
print("\nIndicating duplicate values except the last occurrence...\n", index.duplicated(keep='last'))
输出
这将产生以下代码 −
Pandas Index with duplicates... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object') dtype 对象... object 获取维度... 1 Indicating duplicate values except the last occurrence... [False False True False False]
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

