[Python] Pandas的delete、drop函数的用法

drop函数

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

这是drop函数的所有参数

  • labels是指要删除的标签,一个或者是列表形式的多个;
  • axis是指处哪一个轴;
  • columns是指某一列或者多列;
  • level是指等级,针对多重索引的情况;
  • inplaces是否替换原来的dataframe;

具体更详细的可以参阅官网:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

Axis(轴)含义

axis=0指的是逐行,axis=1指的是逐列。

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], columns=["col1", "col2", "col3", "col4"])
>>> print(df.mean(axis=0))
col1    2.0
col2    2.0
col3    2.0
col4    2.0
dtype: float64
>>> print(df.mean(axis=1))
0    1.0
1    2.0
2    3.0
dtype: float64
>>> print(df.drop(0,axis=0))
   col1  col2  col3  col4
1     2     2     2     2
2     3     3     3     3
>>> print(df.drop(['col1'],axis=1))
   col2  col3  col4
0     1     1     1
1     2     2     2
2     3     3     3

根据结果:

mean(axis=0)计算的是每一列平均值,
mean(axis=1)计算的是每一行平均值。
drop(0,axis=0)删除行,
drop([‘col1’],axis=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
#指定删除相关的列,没有带columns,所以要指出是哪个轴上的
>>> df.drop(['B', 'C'], axis=1)
   A   D
0  0   3
1  4   7
2  8  11
#这里带有columns,所以不用加上axis参数
>>> df.drop(columns=['B', 'C'])
   A   D
0  0   3
1  4   7
2  8  11
 
#删除指定索引的行,这里没有axis参数,就是默认axis=0,也就是删除行
>>> df.drop([0, 1])
   A  B   C   D
2  8  9  10  11
 
#多重索引的情况,因为版本问题,有些版本需要把里面的codes改成labels
>>> 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
 
#我这里不加index参数是因为我的版本加上以后会报错,所以在使用时建议先了解一下版本
df.drop('length', level=0)
 
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

delete函数

具体的用法如下:

del df['A']  # 删除A列,会就地修改

另外,可能drop函数相关的函数还有一些dropna()和drop_duplicated()函数,暂不总结了

posted @ 2021-02-09 09:43  哆啦梦乐园  阅读(2649)  评论(0编辑  收藏  举报