Python Pandas - 删除标签的指定列表,制作新的索引
要删除标签的指定列表,制作新的索引,使用 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
# Creating the index
index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])
# Display the index
print("Pandas Index...\n",index)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Return a tuple of the shape of the underlying data
print("\nA tuple of the shape of underlying data...\n",index.shape)
# a list containing labels to be dropped are passed
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')
广告
数据结构
网络化
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP