之前全是有关于添加操作,现在如果要删除一行或者一列呢,可以使用drop方法。
obj = Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
a 0.0
b 1.0
c 2.0
d 3.0
e 4.0
obj2=obj.drop('c')
a 0.0
b 1.0
d 3.0
e 4.0
对于DataFrame,可以从任何坐标轴删除索引值:
data = DataFrame(np.arange(16).reshape((4, 4)),index=['Ohio', 'Colorado', 'Utah', 'New York'],columns=['one', 'two', 'three', 'four'])
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15
data.drop(['Colorado', 'Ohio'])
one two three four
Utah 8 9 10 11
New York 12 13 14 15
data.drop('two', axis=1)
one three four
Ohio 0 2 3
Colorado 4 6 7
Utah 8 10 11
New York 12 14 15
axis=1表示是对列进行操作,axis=0表示是对行
data.drop(['two', 'four'], axis=1)
one three
Ohio 0 2
Colorado 4 6
Utah 8 10
New York 12 14