时间:2021-05-22
本文通过一个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 npimport pandas as pdodata = pd.read_csv('example.csv')odataOut[1]:date spring summer autumn winter0 2000 12.2338809 16.9073011333 15.6923831333 14.08596223331 2001 12.8474805667 16.7504687333 14.5140663667 13.50374562 2002 13.558175 17.2033926 15.6999475 13.23365246673 2003 12.6547247 16.8949153333 15.6614647 12.84347866674 2004 13.2537298 17.0469665667 15.2090537667 14.36479125 2005 13.4443049 16.7459822 16.6221879667 11.61082256676 2006 13.5056956667 16.8335785667 15.4979282 12.19934363337 2007 13.4885262333 16.6677328333 15.8170143667 13.74382168 2008 13.1515319 16.4865069333 15.7295728667 12.93233586679 2009 13.4577154333 16.6392378333 18.2601799667 12.653159433310 2010 13.1945485 16.7286889 15.4263526667 13.883358311 2011 14.3477941667 16.6894210333 14.1765804333 12.366541966712 2012 13.6050867 17.1305677333 14.7179677667 13.292552433313 2013 13.0279078667 17.3861934333 16.2034549667 13.186121333314 2014 12.7466816333 16.5442868667 14.7367682 12.870651246715 2015 13.465904 16.5061231667 12.4424366333 11.018138416 season spring summer autumn winter17 slope 0.037969137402 -0.0116468916667 -0.0791384411275 -0.0776527455294.drop()方法如果不设置参数inplace=True,则只能在生成的新数据块中实现删除效果,而不能删除原有数据块的相应行。
In [2]:data = odata.drop([16,17])odataOut[2]:date spring summer autumn winter0 2000 12.2338809 16.9073011333 15.6923831333 14.08596223331 2001 12.8474805667 16.7504687333 14.5140663667 13.50374562 2002 13.558175 17.2033926 15.6999475 13.23365246673 2003 12.6547247 16.8949153333 15.6614647 12.84347866674 2004 13.2537298 17.0469665667 15.2090537667 14.36479125 2005 13.4443049 16.7459822 16.6221879667 11.61082256676 2006 13.5056956667 16.8335785667 15.4979282 12.19934363337 2007 13.4885262333 16.6677328333 15.8170143667 13.74382168 2008 13.1515319 16.4865069333 15.7295728667 12.93233586679 2009 13.4577154333 16.6392378333 18.2601799667 12.653159433310 2010 13.1945485 16.7286889 15.4263526667 13.883358311 2011 14.3477941667 16.6894210333 14.1765804333 12.366541966712 2012 13.6050867 17.1305677333 14.7179677667 13.292552433313 2013 13.0279078667 17.3861934333 16.2034549667 13.186121333314 2014 12.7466816333 16.5442868667 14.7367682 12.870651246715 2015 13.465904 16.5061231667 12.4424366333 11.018138416 season spring summer autumn winter17 slope 0.037969137402 -0.0116468916667 -0.0791384411275 -0.0776527455294In [3]:dataOut[3]:date spring summer autumn winter0 2000 12.2338809 16.9073011333 15.6923831333 14.08596223331 2001 12.8474805667 16.7504687333 14.5140663667 13.50374562 2002 13.558175 17.2033926 15.6999475 13.23365246673 2003 12.6547247 16.8949153333 15.6614647 12.84347866674 2004 13.2537298 17.0469665667 15.2090537667 14.36479125 2005 13.4443049 16.7459822 16.6221879667 11.61082256676 2006 13.5056956667 16.8335785667 15.4979282 12.19934363337 2007 13.4885262333 16.6677328333 15.8170143667 13.74382168 2008 13.1515319 16.4865069333 15.7295728667 12.93233586679 2009 13.4577154333 16.6392378333 18.2601799667 12.653159433310 2010 13.1945485 16.7286889 15.4263526667 13.883358311 2011 14.3477941667 16.6894210333 14.1765804333 12.366541966712 2012 13.6050867 17.1305677333 14.7179677667 13.292552433313 2013 13.0279078667 17.3861934333 16.2034549667 13.186121333314 2014 12.7466816333 16.5442868667 14.7367682 12.870651246715 2015 13.465904 16.5061231667 12.4424366333 11.0181384如果inplace=True则原有数据块的相应行被删除
In [4]:odata.drop(odata.index[[16,17]],inplace=True)odataOut[4]:date spring summer autumn winter0 2000 12.2338809 16.9073011333 15.6923831333 14.08596223331 2001 12.8474805667 16.7504687333 14.5140663667 13.50374562 2002 13.558175 17.2033926 15.6999475 13.23365246673 2003 12.6547247 16.8949153333 15.6614647 12.84347866674 2004 13.2537298 17.0469665667 15.2090537667 14.36479125 2005 13.4443049 16.7459822 16.6221879667 11.61082256676 2006 13.5056956667 16.8335785667 15.4979282 12.19934363337 2007 13.4885262333 16.6677328333 15.8170143667 13.74382168 2008 13.1515319 16.4865069333 15.7295728667 12.93233586679 2009 13.4577154333 16.6392378333 18.2601799667 12.653159433310 2010 13.1945485 16.7286889 15.4263526667 13.883358311 2011 14.3477941667 16.6894210333 14.1765804333 12.366541966712 2012 13.6050867 17.1305677333 14.7179677667 13.292552433313 2013 13.0279078667 17.3861934333 16.2034549667 13.186121333314 2014 12.7466816333 16.5442868667 14.7367682 12.870651246715 2015 13.465904 16.5061231667 12.4424366333 11.0181384删除列
del方法
In [5]:del odata['date']odataOut[5]:spring summer autumn winter0 12.2338809 16.9073011333 15.6923831333 14.08596223331 12.8474805667 16.7504687333 14.5140663667 13.50374562 13.558175 17.2033926 15.6999475 13.23365246673 12.6547247 16.8949153333 15.6614647 12.84347866674 13.2537298 17.0469665667 15.2090537667 14.36479125 13.4443049 16.7459822 16.6221879667 11.61082256676 13.5056956667 16.8335785667 15.4979282 12.19934363337 13.4885262333 16.6677328333 15.8170143667 13.74382168 13.1515319 16.4865069333 15.7295728667 12.93233586679 13.4577154333 16.6392378333 18.2601799667 12.653159433310 13.1945485 16.7286889 15.4263526667 13.883358311 14.3477941667 16.6894210333 14.1765804333 12.366541966712 13.6050867 17.1305677333 14.7179677667 13.292552433313 13.0279078667 17.3861934333 16.2034549667 13.186121333314 12.7466816333 16.5442868667 14.7367682 12.870651246715 13.465904 16.5061231667 12.4424366333 11.0181384.pop()方法
.pop方法可以将所选列从原数据块中弹出,原数据块不再保留该列
In [6]:spring = odata.pop('spring')springOut[6]:0 12.23388091 12.84748056672 13.5581753 12.65472474 13.25372985 13.44430496 13.50569566677 13.48852623338 13.15153199 13.457715433310 13.194548511 14.347794166712 13.605086713 13.027907866714 12.746681633315 13.465904Name: spring, dtype: objectIn [7]:odataOut[7]:summer autumn winter0 16.9073011333 15.6923831333 14.08596223331 16.7504687333 14.5140663667 13.50374562 17.2033926 15.6999475 13.23365246673 16.8949153333 15.6614647 12.84347866674 17.0469665667 15.2090537667 14.36479125 16.7459822 16.6221879667 11.61082256676 16.8335785667 15.4979282 12.19934363337 16.6677328333 15.8170143667 13.74382168 16.4865069333 15.7295728667 12.93233586679 16.6392378333 18.2601799667 12.653159433310 16.7286889 15.4263526667 13.883358311 16.6894210333 14.1765804333 12.366541966712 17.1305677333 14.7179677667 13.292552433313 17.3861934333 16.2034549667 13.186121333314 16.5442868667 14.7367682 12.870651246715 16.5061231667 12.4424366333 11.0181384.drop()方法
drop方法既可以保留原数据块中的所选列,也可以删除,这取决于参数inplace
In [8]:withoutSummer = odata.drop(['summer'],axis=1)withoutSummerOut[8]:autumn winter0 15.6923831333 14.08596223331 14.5140663667 13.50374562 15.6999475 13.23365246673 15.6614647 12.84347866674 15.2090537667 14.36479125 16.6221879667 11.61082256676 15.4979282 12.19934363337 15.8170143667 13.74382168 15.7295728667 12.93233586679 18.2601799667 12.653159433310 15.4263526667 13.883358311 14.1765804333 12.366541966712 14.7179677667 13.292552433313 16.2034549667 13.186121333314 14.7367682 12.870651246715 12.4424366333 11.0181384In [9]:odataOut[9]:summer autumn winter0 16.9073011333 15.6923831333 14.08596223331 16.7504687333 14.5140663667 13.50374562 17.2033926 15.6999475 13.23365246673 16.8949153333 15.6614647 12.84347866674 17.0469665667 15.2090537667 14.36479125 16.7459822 16.6221879667 11.61082256676 16.8335785667 15.4979282 12.19934363337 16.6677328333 15.8170143667 13.74382168 16.4865069333 15.7295728667 12.93233586679 16.6392378333 18.2601799667 12.653159433310 16.7286889 15.4263526667 13.883358311 16.6894210333 14.1765804333 12.366541966712 17.1305677333 14.7179677667 13.292552433313 17.3861934333 16.2034549667 13.186121333314 16.5442868667 14.7367682 12.870651246715 16.5061231667 12.4424366333 11.0181384当inplace=True时.drop()执行内部删除,不返回任何值,原数据发生改变
In [10]:withoutWinter = odata.drop(['winter'],axis=1,inplace=True)type(withoutWinter)Out[10]:NoneTypeIn [11]:odataOut[11]:summer autumne0 16.9073011333 15.69238313331 16.7504687333 14.51406636672 17.2033926 15.69994753 16.8949153333 15.66146474 17.0469665667 15.20905376675 16.7459822 16.62218796676 16.8335785667 15.49792827 16.6677328333 15.81701436678 16.4865069333 15.72957286679 16.6392378333 18.260179966710 16.7286889 15.426352666711 16.6894210333 14.176580433312 17.1305677333 14.717967766713 17.3861934333 16.203454966714 16.5442868667 14.736768215 16.5061231667 12.4424366333总结,不论是行删除还是列删除,也不论是原数据删除,还是输出新变量删除,.drop()的方法都能达到目的,为了方便好记,熟练操作,所以应该尽量多使用.drop()方法
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
开始之前,pandas中DataFrame删除对象可能存在几种情况1、删除具体列2、删除具体行3、删除包含某些数值的行或者列4、删除包含某些字符、文字的行或者列
需求:给定一个dataframe和一个list,list中存放的是dataframe中某一列的元素,删除dataframe中与list元素重复的行(即取差集)。
1.摘要dropna()方法,能够找到DataFrame类型数据的空值(缺失值),将空值所在的行/列删除后,将新的DataFrame作为返回值返回。2.函数详解
用pandas中的DataFrame时选取行或列:importnumpyasnpimportpandasaspdfrompandasimportSereis,D
用pandas中的DataFrame时选取行或列:importnumpyasnpimportpandasaspdfrompandasimportSereis,D