[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()函數,暫不總結了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM