1. 程式人生 > >Pandas.DataFrame刪除行和列

Pandas.DataFrame刪除行和列

本文通過一個csv例項檔案來展示如何刪除Pandas.DataFrame的行和列
資料檔名為:example.csv
內容為:

|date|spring|summer|autumn|winter|
|----|
|2000|12.2338809|16.90730113|15.69238313|14.08596223|
|2001|12.84748057|16.75046873|14.51406637| 13.5037456
|2002|13.558175|17.2033926|15.6999475 |13.23365247
|2003|12.6547247|16.89491533|15.6614647 |12.84347867
|2004|13.2537298|17.04696657|15.20905377| 14.3647912
|2005|13.4443049|16.7459822|16.62218797 |11.61082257
|2006|13.50569567|16.83357857| 15.4979282 |12.19934363
|2007|13.48852623|16.66773283| 15.81701437 |13.7438216
|2008|13.1515319|16.48650693 |15.72957287 |12.93233587
|2009|13.45771543|16.63923783 |18.26017997| 12.65315943
|2010|13.1945485|16.7286889|15.42635267 |13.8833583|
|2011|14.34779417|16.68942103 |14.17658043 |12.36654197
|2012|13.6050867|17.13056773 |14.71796777 |13.29255243
|2013|13.02790787|17.38619343 |16.20345497 |13.18612133
|2014|12.74668163|16.54428687|14.7367682|12.87065125|
|2015|13.465904|16.50612317 |12.44243663| 11.0181384
|season|spring|summer |autumn| winter|
|slope|0.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.DataFrame刪除

本文通過一個csv例項檔案來展示如何刪除Pandas.DataFrame的行和列資料檔名為:example.csv內容為:|date|spring|summer|autumn|winter||----||2000|12.2338809|16.90730113|15.69238313|14.08596223||

pandas dataframe 新增

import numpy as np import pandas as pd df=pd.DataFrame(np.random.randn(3,4),columns=list("ABCD"),index=list("xyz")) # print(df) res1=df.apply(lambda

python中pandas庫中DataFrame的操作使用方法

用pandas中的DataFrame時選取行或列:import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser = Series(np.arange(3.)) data =

Grid Compression(刪除)

問題 B: Grid Compression 時間限制: 1 Sec  記憶體限制: 128 MB 題目描述 There is a grid of squares with H horizontal rows and W vertical colu

dataframe進行遍歷修改

首先先定一個這樣的字典,然後我們用不同的方法對其遍歷和修改 字典df df=pd.DataFrame({"A":[1,2,3,4],"B":[5,6,7,8],"C":[1,1,1,1]}) A B C 0 1 5 1 1 2 6 1 2 3 7 1 3

python中pandas.DataFrame求和及新增新示例

本文介紹的是python中pandas.DataFrame對行與列求和及新增新行與列的相關資料,下面話不多說,來看看詳細的介紹吧。 方法如下: 匯入模組: ? 1 2 3 from pandas import DataFrame import panda

Python中pandas dataframe刪除一行或一:drop函式

用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False) 在這裡預設:axis=0,指刪除index,因此刪除columns時要指定axis=1; inplace=False,預設該刪除操作不

Pandas入門基礎(二):DataFrame與資料型別

建立DataFrame資料: data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'], 'year': [2000, 2001, 2002, 2001, 20

Pandas基礎(二):DataFrame與資料型別

建立DataFrame資料: data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'], 'year': [2000, 2001, 2002, 2001, 2002, 2003],

HTML動態增加刪除表格的

<!doctype html> <html> <head> <!--動態生成指定行數和列數的可編輯表格--> <style> </style> <script> function addrow(){   var c=docum

js動態刪除table表的指定

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html

pandas 獲取數據幀DataFrame

das pre row 技術分享 mage object 獲取數據 inf shape 1、創建數據幀 import pandas as pd df = pd.DataFrame([[1, ‘A‘, ‘3%‘ ], [2, ‘B‘]], index=[‘row_0‘

JS 合並單元格相同內容

del nbsp ext 合並單元格 color class doc nal div <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD&

Excel刪除空白行

image last fun solid ast ack color tro ont Sub DeleteEmptyRows() Dim LastRow As Long, r As Long LastRow = ActiveSheet.UsedRange.Rows.Coun

Linux提取不匹配字符串的(awk函數)

div ont 字符串 pre IT int nan span http 如下圖所示,想把含有‘-nan’字符串的行提取出來 則用到awk函數,命令行如下: awk ‘{if($3!="-nan"){print $3}}‘ CHB_vs_

C#中關於DataGridView的背景色-前景色設置

ring strong style屬性 背景 car gray bind etl pin 關於DataGridView行和列的背景色-前景色設置 1.設定DataGridView全部單元格的Style DataGridView內所有單元格的Style變更,可以使用Data

Excel表中如何同時鎖定

info 固定 tro nbsp img image 我們 三種 str 鎖定行列信息欄,需要用到的是視圖工具欄,點擊視圖工具欄我們顯而易見的看到了凍結窗口一欄,通過凍結窗口來鎖定行及列信息欄; 第一種是凍結首行 第二種是凍結首列 通上 第三種是同時凍結前幾

js 遍歷

tag pre tex innertext color 遍歷 bsp tab ext var table = document.getElementsByTagName("table")[0]; //遍歷列 for (var i = 0; i <

如何叠代pandas dataframe

csdn span ring erl ble .net sdn test uil from:https://blog.csdn.net/tanzuozhev/article/details/76713387 How to iterate over rows in a Da

Pandas DataFrame 資料選取過濾

This would allow chaining operations like: pd.read_csv('imdb.txt') .sort(columns='year') .filter(lambda x: x['year']>1990) # <---this is missin