使用DataFrame刪除行和列的例項講解

NO IMAGE

本文通過一個csv例項檔案來展示如何刪除Pandas.DataFrame的行和列

資料檔名為:example.csv

內容為:

datespringsummerautumnwinter
200012.233880916.9073011315.6923831314.08596223
200112.8474805716.7504687314.5140663713.5037456
200213.55817517.203392615.699947513.23365247
200312.654724716.8949153315.661464712.84347867
200413.253729817.0469665715.2090537714.3647912
200513.444304916.745982216.6221879711.61082257
200613.5056956716.8335785715.497928212.19934363
200713.4885262316.6677328315.8170143713.7438216
200813.151531916.4865069315.7295728712.93233587
200913.4577154316.6392378318.2601799712.65315943
201013.194548516.728688915.4263526713.8833583
201114.3477941716.6894210314.1765804312.36654197
201213.605086717.1305677314.7179677713.29255243
201313.0279078717.3861934316.2034549713.18612133
201412.7466816316.5442868714.736768212.87065125
201513.46590416.5061231712.4424366311.0181384
seasonspringsummerautumnwinter
slope0.0379691374-0.01164689167-0.07913844113-0.07765274553

刪除行


In [1]:
import numpy as np
import pandas as pd
odata = pd.read_csv('example.csv')
odata
Out[1]:
date  spring  summer  autumn  winter
0  2000  12.2338809  16.9073011333  15.6923831333  14.0859622333
1  2001  12.8474805667  16.7504687333  14.5140663667  13.5037456
2  2002  13.558175  17.2033926  15.6999475  13.2336524667
3  2003  12.6547247  16.8949153333  15.6614647  12.8434786667
4  2004  13.2537298  17.0469665667  15.2090537667  14.3647912
5  2005  13.4443049  16.7459822  16.6221879667  11.6108225667
6  2006  13.5056956667  16.8335785667  15.4979282  12.1993436333
7  2007  13.4885262333  16.6677328333  15.8170143667  13.7438216
8  2008  13.1515319  16.4865069333  15.7295728667  12.9323358667
9  2009  13.4577154333  16.6392378333  18.2601799667  12.6531594333
10  2010  13.1945485  16.7286889  15.4263526667  13.8833583
11  2011  14.3477941667  16.6894210333  14.1765804333  12.3665419667
12  2012  13.6050867  17.1305677333  14.7179677667  13.2925524333
13  2013  13.0279078667  17.3861934333  16.2034549667  13.1861213333
14  2014  12.7466816333  16.5442868667  14.7367682  12.8706512467
15  2015  13.465904  16.5061231667  12.4424366333  11.0181384
16  season  spring  summer  autumn  winter
17  slope  0.037969137402  -0.0116468916667  -0.0791384411275  -0.0776527455294

.drop()方法如果不設定引數inplace=True,則只能在生成的新資料塊中實現刪除效果,而不能刪除原有資料塊的相應行。


In [2]:
data = odata.drop([16,17])
odata
Out[2]:
date  spring  summer  autumn  winter
0  2000  12.2338809  16.9073011333  15.6923831333  14.0859622333
1  2001  12.8474805667  16.7504687333  14.5140663667  13.5037456
2  2002  13.558175  17.2033926  15.6999475  13.2336524667
3  2003  12.6547247  16.8949153333  15.6614647  12.8434786667
4  2004  13.2537298  17.0469665667  15.2090537667  14.3647912
5  2005  13.4443049  16.7459822  16.6221879667  11.6108225667
6  2006  13.5056956667  16.8335785667  15.4979282  12.1993436333
7  2007  13.4885262333  16.6677328333  15.8170143667  13.7438216
8  2008  13.1515319  16.4865069333  15.7295728667  12.9323358667
9  2009  13.4577154333  16.6392378333  18.2601799667  12.6531594333
10  2010  13.1945485  16.7286889  15.4263526667  13.8833583
11  2011  14.3477941667  16.6894210333  14.1765804333  12.3665419667
12  2012  13.6050867  17.1305677333  14.7179677667  13.2925524333
13  2013  13.0279078667  17.3861934333  16.2034549667  13.1861213333
14  2014  12.7466816333  16.5442868667  14.7367682  12.8706512467
15  2015  13.465904  16.5061231667  12.4424366333  11.0181384
16  season  spring  summer  autumn  winter
17  slope  0.037969137402  -0.0116468916667  -0.0791384411275  -0.0776527455294
In [3]:
data
Out[3]:
date  spring  summer  autumn  winter
0  2000  12.2338809  16.9073011333  15.6923831333  14.0859622333
1  2001  12.8474805667  16.7504687333  14.5140663667  13.5037456
2  2002  13.558175  17.2033926  15.6999475  13.2336524667
3  2003  12.6547247  16.8949153333  15.6614647  12.8434786667
4  2004  13.2537298  17.0469665667  15.2090537667  14.3647912
5  2005  13.4443049  16.7459822  16.6221879667  11.6108225667
6  2006  13.5056956667  16.8335785667  15.4979282  12.1993436333
7  2007  13.4885262333  16.6677328333  15.8170143667  13.7438216
8  2008  13.1515319  16.4865069333  15.7295728667  12.9323358667
9  2009  13.4577154333  16.6392378333  18.2601799667  12.6531594333
10  2010  13.1945485  16.7286889  15.4263526667  13.8833583
11  2011  14.3477941667  16.6894210333  14.1765804333  12.3665419667
12  2012  13.6050867  17.1305677333  14.7179677667  13.2925524333
13  2013  13.0279078667  17.3861934333  16.2034549667  13.1861213333
14  2014  12.7466816333  16.5442868667  14.7367682  12.8706512467
15  2015  13.465904  16.5061231667  12.4424366333  11.0181384

如果inplace=True則原有資料塊的相應行被刪除


In [4]:
odata.drop(odata.index[[16,17]],inplace=True)
odata
Out[4]:
date  spring  summer  autumn  winter
0  2000  12.2338809  16.9073011333  15.6923831333  14.0859622333
1  2001  12.8474805667  16.7504687333  14.5140663667  13.5037456
2  2002  13.558175  17.2033926  15.6999475  13.2336524667
3  2003  12.6547247  16.8949153333  15.6614647  12.8434786667
4  2004  13.2537298  17.0469665667  15.2090537667  14.3647912
5  2005  13.4443049  16.7459822  16.6221879667  11.6108225667
6  2006  13.5056956667  16.8335785667  15.4979282  12.1993436333
7  2007  13.4885262333  16.6677328333  15.8170143667  13.7438216
8  2008  13.1515319  16.4865069333  15.7295728667  12.9323358667
9  2009  13.4577154333  16.6392378333  18.2601799667  12.6531594333
10  2010  13.1945485  16.7286889  15.4263526667  13.8833583
11  2011  14.3477941667  16.6894210333  14.1765804333  12.3665419667
12  2012  13.6050867  17.1305677333  14.7179677667  13.2925524333
13  2013  13.0279078667  17.3861934333  16.2034549667  13.1861213333
14  2014  12.7466816333  16.5442868667  14.7367682  12.8706512467
15  2015  13.465904  16.5061231667  12.4424366333  11.0181384

刪除列

del方法


In [5]:
del odata['date']
odata
Out[5]:
spring  summer  autumn  winter
0  12.2338809  16.9073011333  15.6923831333  14.0859622333
1  12.8474805667  16.7504687333  14.5140663667  13.5037456
2  13.558175  17.2033926  15.6999475  13.2336524667
3  12.6547247  16.8949153333  15.6614647  12.8434786667
4  13.2537298  17.0469665667  15.2090537667  14.3647912
5  13.4443049  16.7459822  16.6221879667  11.6108225667
6  13.5056956667  16.8335785667  15.4979282  12.1993436333
7  13.4885262333  16.6677328333  15.8170143667  13.7438216
8  13.1515319  16.4865069333  15.7295728667  12.9323358667
9  13.4577154333  16.6392378333  18.2601799667  12.6531594333
10  13.1945485  16.7286889  15.4263526667  13.8833583
11  14.3477941667  16.6894210333  14.1765804333  12.3665419667
12  13.6050867  17.1305677333  14.7179677667  13.2925524333
13  13.0279078667  17.3861934333  16.2034549667  13.1861213333
14  12.7466816333  16.5442868667  14.7367682  12.8706512467
15  13.465904  16.5061231667  12.4424366333  11.0181384

.pop()方法

.pop方法可以將所選列從原資料塊中彈出,原資料塊不再保留該列


In [6]:
spring = odata.pop('spring')
spring
Out[6]:
0    12.2338809
1   12.8474805667
2     13.558175
3    12.6547247
4    13.2537298
5    13.4443049
6   13.5056956667
7   13.4885262333
8    13.1515319
9   13.4577154333
10    13.1945485
11  14.3477941667
12    13.6050867
13  13.0279078667
14  12.7466816333
15    13.465904
Name: spring, dtype: object
In [7]:
odata
Out[7]:
summer  autumn  winter
0  16.9073011333  15.6923831333  14.0859622333
1  16.7504687333  14.5140663667  13.5037456
2  17.2033926  15.6999475  13.2336524667
3  16.8949153333  15.6614647  12.8434786667
4  17.0469665667  15.2090537667  14.3647912
5  16.7459822  16.6221879667  11.6108225667
6  16.8335785667  15.4979282  12.1993436333
7  16.6677328333  15.8170143667  13.7438216
8  16.4865069333  15.7295728667  12.9323358667
9  16.6392378333  18.2601799667  12.6531594333
10  16.7286889  15.4263526667  13.8833583
11  16.6894210333  14.1765804333  12.3665419667
12  17.1305677333  14.7179677667  13.2925524333
13  17.3861934333  16.2034549667  13.1861213333
14  16.5442868667  14.7367682  12.8706512467
15  16.5061231667  12.4424366333  11.0181384

.drop()方法

drop方法既可以保留原資料塊中的所選列,也可以刪除,這取決於引數inplace


In [8]:
withoutSummer = odata.drop(['summer'],axis=1)
withoutSummer
Out[8]:
autumn  winter
0  15.6923831333  14.0859622333
1  14.5140663667  13.5037456
2  15.6999475  13.2336524667
3  15.6614647  12.8434786667
4  15.2090537667  14.3647912
5  16.6221879667  11.6108225667
6  15.4979282  12.1993436333
7  15.8170143667  13.7438216
8  15.7295728667  12.9323358667
9  18.2601799667  12.6531594333
10  15.4263526667  13.8833583
11  14.1765804333  12.3665419667
12  14.7179677667  13.2925524333
13  16.2034549667  13.1861213333
14  14.7367682  12.8706512467
15  12.4424366333  11.0181384
In [9]:
odata
Out[9]:
summer  autumn  winter
0  16.9073011333  15.6923831333  14.0859622333
1  16.7504687333  14.5140663667  13.5037456
2  17.2033926  15.6999475  13.2336524667
3  16.8949153333  15.6614647  12.8434786667
4  17.0469665667  15.2090537667  14.3647912
5  16.7459822  16.6221879667  11.6108225667
6  16.8335785667  15.4979282  12.1993436333
7  16.6677328333  15.8170143667  13.7438216
8  16.4865069333  15.7295728667  12.9323358667
9  16.6392378333  18.2601799667  12.6531594333
10  16.7286889  15.4263526667  13.8833583
11  16.6894210333  14.1765804333  12.3665419667
12  17.1305677333  14.7179677667  13.2925524333
13  17.3861934333  16.2034549667  13.1861213333
14  16.5442868667  14.7367682  12.8706512467
15  16.5061231667  12.4424366333  11.0181384

當inplace=True時.drop()執行內部刪除,不返回任何值,原資料發生改變


In [10]:
withoutWinter = odata.drop(['winter'],axis=1,inplace=True)
type(withoutWinter)
Out[10]:
NoneType
In [11]:
odata
Out[11]:
summer  autumne
0  16.9073011333  15.6923831333
1  16.7504687333  14.5140663667
2  17.2033926  15.6999475
3  16.8949153333  15.6614647
4  17.0469665667  15.2090537667
5  16.7459822  16.6221879667
6  16.8335785667  15.4979282
7  16.6677328333  15.8170143667
8  16.4865069333  15.7295728667
9  16.6392378333  18.2601799667
10  16.7286889  15.4263526667
11  16.6894210333  14.1765804333
12  17.1305677333  14.7179677667
13  17.3861934333  16.2034549667
14  16.5442868667  14.7367682
15  16.5061231667  12.4424366333

總結,不論是行刪除還是列刪除,也不論是原資料刪除,還是輸出新變數刪除,.drop()的方法都能達到目的,為了方便好記,熟練操作,所以應該儘量多使用.drop()方法

您可能感興趣的文章:

pandas按若干個列的組合條件篩選資料的方法python pandas中DataFrame型別資料操作函式的方法pandas.DataFrame 根據條件新建列並賦值的方法python pandas dataframe 按列或者按行合併的方法pandas系列之DataFrame 行列資料篩選例項