一、drop方法,丢弃指定轴上的项
drop方法可以对Series对象删除某项索引值。
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
obj = Series(np.arange(5.), index = ['a', 'b', 'c', 'd', 'e'])
new_obj = obj.drop('c')
new_obj
输出:
a 0.0
b 1.0
d 3.0
e 4.0
dtype: float64
obj.drop(['d', 'c'])
输出:
a 0.0
b 1.0
e 4.0
dtype: float64
drop方法可以对DataFrame对象删除某项索引值,包括行索引(index, axis = 0)和列索引(columns, axis = 1)。
data = DataFrame(np.arange(16).reshape((4, 4)),index = ['Ohio', 'Colorado', 'Utah', 'New York'], columns = ['one', 'two', 'three', 'four'])
data.drop(['Colorado', 'Ohio'], axis = 0)
输出:
one two three four
Utah 8 9 10 11
New York 12 13 14 15
data.drop(['one', 'two'], axis = 1)
输出:
three four
Ohio 2 3
Colorado 6 7
Utah 10 11
New York 14 15
开工了就只有碎片时间学习了,每篇的篇幅短了好多。
网友评论