首页 文章资讯内容详情

Python Pandas - 使用已删除的标签列表创建新索引

2026-06-03 1 花语

要使用删除的标签列表创建新索引,请使用该方法。在其中传递标签列表。index.drop()

首先,导入所需的库-

import pandas as pd

创建索引-

index = pd.Index([Car,Bike,Truck,Ship,Airplane])

显示索引-

print("Pandas Index...\n",index)

包含要删除的标签的列表被传递-

print("\nUpdated index after deleting labels...\n",index.drop([Bike, Ship]))

示例

以下是代码-

import pandas as pd #创建索引 index = pd.Index([Car,Bike,Truck,Ship,Airplane]) #显示索引 print("Pandas Index...\n",index) #返回数据的dtype print("\nThe dtype object...\n",index.dtype) #返回基础数据形状的元组 print("\nA tuple of the shape of underlying data...\n",index.shape) #传递包含要删除的标签的列表 print("\nUpdated index after deleting labels...\n",index.drop([Bike, Ship]))输出结果

这将产生以下代码-

Pandas Index... Index([Car, Bike, Truck, Ship, Airplane], dtype=object) The dtype object... object A tuple of the shape of underlying data... (5,) Updated index after deleting labels... Index([Car, Truck, Airplane], dtype=object)