根据列值删除Pandas中的DataFrame行
本文翻译自:Deleting DataFrame row in Pandas based on column value
I have the following DataFrame: 我有以下DataFrame:
daysago line_race rating rw wratingline_date 2007-03-31 62 11 56 1.000000 56.0000002007-03-10 83 11 67 1.000000 67.0000002007-02-10 111 9 66 1.000000 66.0000002007-01-13 139 10 83 0.880678 73.0962782006-12-23 160 10 88 0.793033 69.7869422006-11-09 204 9 52 0.636655 33.1060772006-10-22 222 8 66 0.581946 38.4084082006-09-29 245 9 70 0.518825 36.3177522006-09-16 258 11 68 0.486226 33.0633812006-08-30 275 8 72 0.446667 32.1600512006-02-11 475 5 65 0.164591 10.6984232006-01-13 504 0 70 0.142409 9.9686342006-01-02 515 0 64 0.134800 8.6272192005-12-06 542 0 70 0.117803 8.2462382005-11-29 549 0 70 0.113758 7.9630722005-11-22 556 0 -1 0.109852 -0.1098522005-11-01 577 0 -1 0.098919 -0.0989192005-10-20 589 0 -1 0.093168 -0.0931682005-09-27 612 0 -1 0.083063 -0.0830632005-09-07 632 0 -1 0.075171 -0.0751712005-06-12 719 0 69 0.048690 3.3596232005-05-29 733 0 -1 0.045404 -0.0454042005-05-02 760 0 -1 0.039679 -0.0396792005-04-02 790 0 -1 0.034160 -0.0341602005-03-13 810 0 -1 0.030915 -0.0309152004-11-09 934 0 -1 0.016647 -0.016647
I need to remove the rows where line_race
is equal to 0
. 我需要删除line_race
等于0
的行。 What's the most efficient way to do this? 最有效的方法是什么?
#1楼
参考:https://stackoom.com/question/1EFaV/根据列值删除Pandas中的DataFrame行
#2楼
如果我理解正确,它应该很简单:
df = df[df.line_race != 0]
#3楼
The best way to do this is with boolean masking: 最好的方法是使用布尔掩码:
In [56]: df
Out[56]:line_date daysago line_race rating raw wrating
0 2007-03-31 62 11 56 1.000 56.000
1 2007-03-10 83 11 67 1.000 67.000
2 2007-02-10 111 9 66 1.000 66.000
3 2007-01-13 139 10 83 0.881 73.096
4 2006-12-23 160 10 88 0.793 69.787
5 2006-11-09 204 9 52 0.637 33.106
6 2006-10-22 222 8 66 0.582 38.408
7 2006-09-29 245 9 70 0.519 36.318
8 2006-09-16 258 11 68 0.486 33.063
9 2006-08-30 275 8 72 0.447 32.160
10 2006-02-11 475 5 65 0.165 10.698
11 2006-01-13 504 0 70 0.142 9.969
12 2006-01-02 515 0 64 0.135 8.627
13 2005-12-06 542 0 70 0.118 8.246
14 2005-11-29 549 0 70 0.114 7.963
15 2005-11-22 556 0 -1 0.110 -0.110
16 2005-11-01 577 0 -1 0.099 -0.099
17 2005-10-20 589 0 -1 0.093 -0.093
18 2005-09-27 612 0 -1 0.083 -0.083
19 2005-09-07 632 0 -1 0.075 -0.075
20 2005-06-12 719 0 69 0.049 3.360
21 2005-05-29 733 0 -1 0.045 -0.045
22 2005-05-02 760 0 -1 0.040 -0.040
23 2005-04-02 790 0 -1 0.034 -0.034
24 2005-03-13 810 0 -1 0.031 -0.031
25 2004-11-09 934 0 -1 0.017 -0.017In [57]: df[df.line_race != 0]
Out[57]:line_date daysago line_race rating raw wrating
0 2007-03-31 62 11 56 1.000 56.000
1 2007-03-10 83 11 67 1.000 67.000
2 2007-02-10 111 9 66 1.000 66.000
3 2007-01-13 139 10 83 0.881 73.096
4 2006-12-23 160 10 88 0.793 69.787
5 2006-11-09 204 9 52 0.637 33.106
6 2006-10-22 222 8 66 0.582 38.408
7 2006-09-29 245 9 70 0.519 36.318
8 2006-09-16 258 11 68 0.486 33.063
9 2006-08-30 275 8 72 0.447 32.160
10 2006-02-11 475 5 65 0.165 10.698
UPDATE: Now that pandas 0.13 is out, another way to do this is df.query('line_race != 0')
. 更新:现在熊猫0.13了,另一种方法是df.query('line_race != 0')
。
#4楼
But for any future bypassers you could mention that df = df[df.line_race != 0]
doesn't do anything when trying to filter for None
/missing values. 但是对于任何将来的绕过器,您可能会提到df = df[df.line_race != 0]
在尝试过滤None
/ missing值时不执行任何操作。
Does work: 可以工作:
df = df[df.line_race != 0]
Doesn't do anything: 什么都不做:
df = df[df.line_race != None]
Does work: 可以工作:
df = df[df.line_race.notnull()]
#5楼
The given answer is correct nontheless as someone above said you can use df.query('line_race != 0')
which depending on your problem is much faster. 给出的答案仍然是正确的,因为上面的人说您可以使用df.query('line_race != 0')
,这取决于您的问题要快得多。 Highly recommend. 强烈推荐。
#6楼
只是添加另一种解决方案,如果您使用的是新的熊猫评估器,这将特别有用,其他解决方案将取代原始的熊猫并失去评估器
df.drop(df.loc[df['line_race']==0].index, inplace=True)
根据列值删除Pandas中的DataFrame行相关推荐
- dataframe 按条件删行_根据列值删除Pandas中的DataFrame行
最好的方法是使用布尔掩蔽: In [56]: df Out[56]: line_date daysago line_race rating raw wrating 0 2007-03 ...
- python怎么选取不连续的列_用pandas中的DataFrame时选取行或列的方法
如下所示: import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser = Series(np.ar ...
- pandas中关于DataFrame行,列显示不完全的解决方案
1.问题背景 在使用DataFrame的过程中,由于行列数量太多,print打印出来会显示不完全.如图: 2.解决方案 #显示所有列 pd.set_option('display.max_column ...
- python pandas中关于DataFrame行,列显示不完全(省略)的解决办法
引用 添加代码即可: #显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_rows' ...
- python pandas dataframe 排序,如何按两列或更多列对python pandas中的dataFrame进行排序?...
Suppose I have a dataframe with columns a, b and c, I want to sort the dataframe by column b in asce ...
- 在pandas中遍历DataFrame行
有如下 Pandas DataFrame: import pandas as pd inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,' ...
- python dataframe遍历_在pandas中遍历DataFrame行的实现方法
有如下 Pandas DataFrame: import pandas as pd inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,' ...
- pandas 选取第一行_用pandas中的DataFrame时选取行或列的方法
如下所示: import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser = Series(np.ar ...
- python列表按照指定顺序排序-pandas中的DataFrame按指定顺序输出所有列的方法
问题: 输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致. 例如: import pandas as pd grades = [48,99, ...
最新文章
- SpringBoot 2.x 使用 JWT(JSON Web Token)
- Hibernate中的Entity类之间的继承关系之一MappedSuperclass
- SQL 2012 Group By Rollup, Grouping
- Hadoop详解(九):Hadoop Streaming和Pipes原理和实现
- python遍历目录,获取指定文件
- 查询oracle中用户的角色,oracle中用户角色的查询和授权
- DeepEarth更新到Silverlight 4了
- ASP.NET Core2.2 和2.1 版本中对cookie的设置和存储
- springcloud 高可用的服务注册中心
- mysql数据库运维_开源数据库MySQL DBA运维实战3
- javamailsender注入失败_springboot拦截异常信息发送邮件提醒
- 三层交换机VLAN间路由
- 查询时间转换成短格式
- android系统电源管理驱动分析
- 金仓数据库KingbaseES实现主键自增的方式
- 纹理(讲得比较详细的文章)
- 基于改进U-GAT-IT的人像手绘风格转换系统(源码&教程)
- MySQL,从删库到跑路
- 赵小楼《天道》《遥远的救世主》深度解析(9)肖亚文的“小心思和小算计”
- centos 防火墙关闭 不能访问3306端口