您的位置:首页 > 编程语言 > Python开发

<Python数据分析>pandas, DataFrame, Index的方法delete和drop的区别

2018-01-04 09:16 1521 查看
delete和drop都是Index类删除索引的方法

《利用Python进行数据分析》一书对二者的描述如下

delete 删除索引i处的元素,并得到新的Index
drop 删除传入的值,并得到新的Index事实上,delete接受的参数是数字下标,而drop接受的参数是具体的索引值
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

obj = Series(range(5), index=['a', 'b', 'c', 'd', 'e'])
index = obj.index
print('原索引:', index)
print('方法drop:',index.drop('b'))
print('方法delete:',index.delete(4))输出
原索引: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
方法drop: Index(['a', 'c', 'd', 'e'], dtype='object')
方法delete: Index(['a', 'b', 'c', 'd'], dtype='object')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python pandas