1. 程式人生 > >pandas刪除(drop)方法

pandas刪除(drop)方法

刪除指定軸上的項

即刪除 Series 的元素或 DataFrame 的某一行(列)的意思,通過物件的 .drop(labels, axis=0) 方法:

刪除Series的一個元素:

1234567 In[11]: ser = Series([4.5,7.2,-5.3,3.6], index=['d','b','a','c'])In[13]: ser.drop('c')Out[13]: d 4.5b 7.2a -5.3dtype: float64

刪除DataFrame的行或列:

1234567891011121314151617181920 In[17]: df = DataFrame
(np.arange(9).reshape(3,3), index=['a','c','d'], columns=['oh','te','ca'])In[18]: dfOut[18]: oh te caa 0 1 2c 3 4 5d 6 7 8In[19]: df.drop('a')Out[19]: oh te cac 3 4 5d 6 7 8In[20]: df.drop(['oh','te'],axis=1)Out[20]: caa 2c 5d 8

.drop() 返回的是一個新物件,元物件不會被改變。