pandas Series DataFrame 丟棄指定軸上的項(三)
阿新 • • 發佈:2019-02-11
1.0 刪除Series項
from pandas import Series,DataFrame
import numpy as np
import pandas as pd
obj=Series(np.arange(5),index=['a','b','c','d','e'])
obj
Out[82]:
a 0
b 1
c 2
d 3
e 4
dtype: int32
new_obj=obj.drop('c')
new_obj
Out[84]:
a 0
b 1
d 3
e 4
dtype: int32
obj=Series(np.arange (5),index=['a','b','c','d','e'])
obj
Out[90]:
a 0
b 1
c 2
d 3
e 4
dtype: int32
obj.drop(['d','c'])
Out[91]:
a 0
b 1
e 4
dtype: int32
2.0 刪除DataFrame項
data=DataFrame(np.arange(16).reshape((4,4)),index=['ohio','colorado','utah','new york'],columns=['one','two','three','four' ])
data
Out[94]:
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
刪除 colorado 和ohio
data.drop(['colorado','ohio'])
Out[95]:
one two three four
utah 8 9 10 11
new york 12 13 14 15
data.drop('two',axis=1)
Out[98]:
one three four
ohio 0 2 3
colorado 4 6 7
utah 8 10 11
new york 12 14 15
data.drop(['two','four'],axis=1)
Out[100]:
one three
ohio 0 2
colorado 4 6
utah 8 10
new york 12 14