zoukankan      html  css  js  c++  java
  • df.drop()函数删除多行或者多列

    函数用法

    从行或列中删除指定的标签

    通过指定标签名称和相应的轴,或直接指定索引或列名称,删除行或列。使用多索引时,可以通过指定级别来删除不同级别上的标签

    函数参数

    DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

    参数解释:

    1. labels:单个标签或类似列表,要删除的索引或列标签。
    2. axis:{0或'index',1或'columns'},默认0,是从索引(0或“ index”)还是从列(1或“ columns”)中删除标签。
    3. index:单个标签或类似列表,指定轴的替代方法(labels, axis=0 is equivalent to index=labels
    4. columns:单标签或类似列表,指定轴的替代方法(labels, axis=1 is equivalent to columns=labels)
    5. level:int或级别名称,可选,对于MultiIndex,将从中删除标签的级别。
    6. inplace:布尔值,默认为False,如果为False,则返回副本。否则,执行就地操作并返回无。
    7. errors:{'ignore','raise'},默认为'raise',如果'ignore',则抑制错误,仅删除现有标签 。

    注意:labels和axis的搭配

    例子

    #构建一个表
    df = pd.DataFrame(np.arange(12).reshape(3, 4),
                      columns=['A', 'B', 'C', 'D'])
    df
       A  B   C   D
    0  0  1   2   3
    1  4  5   6   7
    2  8  9  10  11

    删除列,需要注明axis=1或者是columns=xxx

    #一种表达
    df.drop(['B', 'C'], axis=1)
    
    #另一种表达
    df.drop(columns=['B', 'C'])
    
    #还可以这样表达
    df.drop(labels=['B', 'C'], axis=1)
       A   D
    0  0   3
    1  4   7
    2  8  11

    删除行,由于默认是删除行,因此可以这样子表达,

    df.iloc[[0,1],:]   #对行、列进行切片  第1、2行
    df.drop([0, 1])
       A  B   C   D
    2  8  9  10  11

    删除MultiIndex 行或者列

    #新建一个复合索引的表
    midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
                                 ['speed', 'weight', 'length']],
                         codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
                                [0, 1, 2, 0, 1, 2, 0, 1, 2]])
    df = pd.DataFrame(index=midx, columns=['big', 'small'],
                      data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
                            [250, 150], [1.5, 0.8], [320, 250],
                            [1, 0.8], [0.3, 0.2]])
    df
                    big     small
    lama    speed   45.0    30.0
            weight  200.0   100.0
            length  1.5     1.0
    cow     speed   30.0    20.0
            weight  250.0   150.0
            length  1.5     0.8
    falcon  speed   320.0   250.0
            weight  1.0     0.8
            length  0.3     0.2

    删除行

    #删除
    df.drop(index='length', level=1)
                    big     small
    lama    speed   45.0    30.0
            weight  200.0   100.0
    cow     speed   30.0    20.0
            weight  250.0   150.0
    falcon  speed   320.0   250.0
            weight  1.0     0.8

    删除列

    #删除某行某列
    df.drop(index='cow', columns='small')
                    big
    lama    speed   45.0
            weight  200.0
            length  1.5
    falcon  speed   320.0
            weight  1.0
            length  0.3
  • 相关阅读:
    webpack2 前篇
    vue 的调试工具
    CSS 命名规范总结
    reset.css
    推荐几个精致的web UI框架
    自己是个菜鸟 自己查找的简单的适合初学的Makefile
    Linux下编译、使用静态库和动态库 自己测过的
    函数参数的传递 动态内存传递问题(指针的指针)
    二级指针 (C语言)
    find_if查找vector内对象的成员 作为菜鸟一直不会用也不敢用
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/14030237.html
Copyright © 2011-2022 走看看