pandas.DataFrame.drop
DataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level.
这个drop看上去还是挺好用的,我们来试试
df = pd.DataFrame(np.arange(12).reshape(3, 4),
columns=['A', 'B', 'C', 'D'])
![](https://img.haomeiwen.com/i76024/f8e788ee0701d4f0.png)
drop函数的话,也有两种使用方式,一种是使用axis指定轴,或者直接使用index、columns参数来指定
df.drop(['B','D'] , axis=1)
df.drop(columns=['B' , 'D'])
上面这两种方式就是一样的
![](https://img.haomeiwen.com/i76024/ed87087036730ad7.png)
这个drop也不是直接修改原对象,所以其实和筛选函数是一个原理,我们可以通过inplace=True
来直接修改
df.drop(columns=['A' , 'C'] , inplace=True)
![](https://img.haomeiwen.com/i76024/80c572e930e1c236.png)
删除index也是同样的方式
df.drop(0)
df.drop([0,2])
![](https://img.haomeiwen.com/i76024/593d2a53462a72c4.png)
其他几个参数都是很常用的,这里就不多说了
网友评论