美文网首页
pandas删除函数-drop

pandas删除函数-drop

作者: 橘猫吃不胖 | 来源:发表于2020-05-12 15:41 被阅读0次

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'])

drop函数的话,也有两种使用方式,一种是使用axis指定轴,或者直接使用index、columns参数来指定

df.drop(['B','D'] , axis=1)
df.drop(columns=['B' , 'D'])

上面这两种方式就是一样的


这个drop也不是直接修改原对象,所以其实和筛选函数是一个原理,我们可以通过inplace=True来直接修改

df.drop(columns=['A' , 'C'] , inplace=True)

删除index也是同样的方式

df.drop(0)
df.drop([0,2])

其他几个参数都是很常用的,这里就不多说了

相关文章

网友评论

      本文标题:pandas删除函数-drop

      本文链接:https://www.haomeiwen.com/subject/fkibnhtx.html