使用DataFrame删除行和列的实例讲解


Posted in Python onApril 08, 2018

本文通过一个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()方法

Python 相关文章推荐
给Python IDLE加上自动补全和历史功能
Nov 30 Python
Python加密方法小结【md5,base64,sha1】
Jul 13 Python
Python时间的精准正则匹配方法分析
Aug 17 Python
Python3.4实现从HTTP代理网站批量获取代理并筛选的方法示例
Sep 26 Python
Python 3.8中实现functools.cached_property功能
May 29 Python
python3实现猜数字游戏
Dec 07 Python
Python中注释(多行注释和单行注释)的用法实例
Aug 28 Python
关于Python字符串显示u...的解决方式
Mar 06 Python
Matplotlib 绘制饼图解决文字重叠的方法
Jul 24 Python
Python如何爬取b站热门视频并导入Excel
Aug 10 Python
python 线程的五个状态
Sep 22 Python
python-for x in range的用法(注意要点、细节)
May 10 Python
将字典转换为DataFrame并进行频次统计的方法
Apr 08 #Python
pandas创建新Dataframe并添加多行的实例
Apr 08 #Python
DataFrame中去除指定列为空的行方法
Apr 08 #Python
python 定时修改数据库的示例代码
Apr 08 #Python
对Python中DataFrame按照行遍历的方法
Apr 08 #Python
python2.6.6如何升级到python2.7.14
Apr 08 #Python
python解决pandas处理缺失值为空字符串的问题
Apr 08 #Python
You might like
简化php模板页面中分页代码的解析
2009/02/06 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
Thinkphp 框架扩展之标签库驱动原理与用法分析
2020/04/23 PHP
jQuery 1.3 和 Validation 验证插件1.5.1
2009/07/09 Javascript
JavaScript isPrototypeOf和hasOwnProperty使用区别
2010/03/04 Javascript
禁止IE用右键的JS代码
2013/12/30 Javascript
如何实现textarea里的不同文本显示不同颜色
2014/01/20 Javascript
jQuery实现仿Google首页拖动效果的方法
2015/05/04 Javascript
jquery获取url参数及url加参数的方法
2015/10/26 Javascript
JS 全屏和退出全屏详解及实例代码
2016/11/07 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
vue 如何添加全局函数或全局变量以及单页面的title设置总结
2017/06/01 Javascript
js 判断一个数字是不是2的n次方幂的实例
2017/11/26 Javascript
jQuery代码优化方法总结
2018/01/29 jQuery
详解JSON.stringify()的5个秘密特性
2020/05/26 Javascript
Vue实现购物车基本功能
2020/11/08 Javascript
[08:42]DOTA2每周TOP10 精彩击杀集锦vol.2
2014/06/25 DOTA
[47:21]Liquid vs TNC Supermajor 胜者组 BO3 第一场 6.4
2018/06/05 DOTA
Python字符串转换成浮点数函数分享
2015/07/24 Python
Python中Collections模块的Counter容器类使用教程
2016/05/31 Python
python中必要的名词解释
2019/11/20 Python
python不使用for计算两组、多个矩形两两间的iou方式
2020/01/18 Python
Python下使用Trackbar实现绘图板
2020/10/27 Python
python 实现端口扫描工具
2020/12/18 Python
Ticketmaster德国票务网站:购买音乐会和体育等门票
2016/11/14 全球购物
荷兰鞋子在线:Nelson Schoenen
2017/12/25 全球购物
Expedia瑞典官网:预订度假屋、酒店、汽车租赁、机票等
2021/01/23 全球购物
北京捷通华声语音技术有限公司Java软件工程师笔试题
2012/04/10 面试题
一帮一活动总结
2014/05/08 职场文书
理发店策划方案
2014/06/05 职场文书
英语课外活动总结
2014/08/27 职场文书
优秀教师申报材料
2014/12/16 职场文书
酒吧七夕情人节宣传语
2015/11/24 职场文书
2017年寒假社区服务活动总结
2016/04/06 职场文书
浅谈CSS不规则边框的生成方案
2021/05/25 HTML / CSS
JavaWeb Servlet实现网页登录功能
2021/07/04 Java/Android