zoukankan      html  css  js  c++  java
  • Pandas库学习之一:删除

    删除

    drop(...)

        def drop(
            self,
            labels=None,  # 需要删除的标签,一个或者是列表形式的多个
            axis=0,  # 指定给出的labels是在哪个轴上,axis=0表示行,axis=1表示列
            index=None,  # 某一索引或者多个索引
            columns=None,  # 某一列或者多列
            level=None,  # 等级,针对多重索引的情况
            inplace=False,  # 是否替换原来的dataframe
            errors="raise",
        ):
            """
            Examples
            --------
            >>> 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
    
            Drop columns
    
            >>> df.drop(['B', 'C'], axis=1)
               A   D
            0  0   3
            1  4   7
            2  8  11
    
            >>> df.drop(columns=['B', 'C'])
               A   D
            0  0   3
            1  4   7
            2  8  11
    
            Drop a row by index
    
            >>> df.drop([0, 1])
               A  B   C   D
            2  8  9  10  11
    
            Drop columns and/or rows of MultiIndex DataFrame
    
            >>> 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='cow', columns='small')
                            big
            lama    speed   45.0
                    weight  200.0
                    length  1.5
            falcon  speed   320.0
                    weight  1.0
                    length  0.3
    
            >>> 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
            """
            return super().drop(
                labels=labels,
                axis=axis,
                index=index,
                columns=columns,
                level=level,
                inplace=inplace,
                errors=errors,
            )
    
    

    拓展

    1. drop函数删除特定条件的行
    """
    >>> 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
    """
    IN [2]: data.drop(index=0) #删除index=0的行
    Out[2]:  
       A  B   C   D
    1  4  5   6   7
    2  8  9  10  11
     
    IN [3]: data.drop(labels=0, axis=0) #删除 "行号为0" 的行
    Out[3]:  
       A  B   C   D
    1  4  5   6   7
    2  8  9  10  11
    In [4]: data.drop(index=data[data['A'].isin([4])].index) #删除包含4的行
    Out[4]: 
       A  B   C   D
    0  0  1   2   3
    2  8  9  10  11
     
    In [5]: data.drop(index=data[data['A']==4].index) #删除包含4的行
    Out[5]: 
       A  B   C   D
    0  0  1   2   3
    2  8  9  10  11
    
    1. 删除列( 基于Index和columns删除列
    IN [6]: data.drop(columns='A') #删除columns为A的列
    Out[6]:  
       B   C   D
    0  1   2   3
    1  5   6   7
    2  9  10  11
     
    IN [7]: data.drop(labels='A', axis=1) #删除 "列名为A" 的列
    Out[7]:  
       B   C   D
    0  1   2   3
    1  5   6   7
    2  9  10  11
    

    delete(...)

    pop(...)

    pop(self, item: Label) -> Series:
            """
            >>> df
                 name   class  max_speed
            0  falcon    bird      389.0
            1  parrot    bird       24.0
            2    lion  mammal       80.5
            3  monkey  mammal        NaN
    
            >>> df.pop('class')
            0      bird
            1      bird
            2    mammal
            3    mammal
            Name: class, dtype: object
    
            >>> df
                 name  max_speed
            0  falcon      389.0
            1  parrot       24.0
            2    lion       80.5
            3  monkey        NaN
            """
            return super().pop(item=item)
        
    def pop(self, item: Label) -> Union["Series", Any]:
       result = self[item]
       del self[item]
       if self.ndim == 2:
       result._reset_cacher()
    
       return result
    
  • 相关阅读:
    cts 测试环境安装 ubuntu
    关于JS中乘除法的浮点错误解决方法
    事件移除匿名函数
    获取元素offsetLeft值
    jquery on and bind different
    获取事件相对于文档的位置
    angular 使用过程中遇到的问题
    python_文件操作
    monkerunner
    monkey_使用_脚本编写
  • 原文地址:https://www.cnblogs.com/854594834-YT/p/14204470.html
Copyright © 2011-2022 走看看