基于列值删除大熊猫中的dataframe行
我有以下数据框:
daysago line_race rating rw wrating
line_date
2007-03-31 62 11 56 1.000000 56.000000
2007-03-10 83 11 67 1.000000 67.000000
2007-02-10 111 9 66 1.000000 66.000000
2007-01-13 139 10 83 0.880678 73.096278
2006-12-23 160 10 88 0.793033 69.786942
2006-11-09 204 9 52 0.636655 33.106077
2006-10-22 222 8 66 0.581946 38.408408
2006-09-29 245 9 70 0.518825 36.317752
2006-09-16 258 11 68 0.486226 33.063381
2006-08-30 275 8 72 0.446667 32.160051
2006-02-11 475 5 65 0.164591 10.698423
2006-01-13 504 0 70 0.142409 9.968634
2006-01-02 515 0 64 0.134800 8.627219
2005-12-06 542 0 70 0.117803 8.246238
2005-11-29 549 0 70 0.113758 7.963072
2005-11-22 556 0 -1 0.109852 -0.109852
2005-11-01 577 0 -1 0.098919 -0.098919
2005-10-20 589 0 -1 0.093168 -0.093168
2005-09-27 612 0 -1 0.083063 -0.083063
2005-09-07 632 0 -1 0.075171 -0.075171
2005-06-12 719 0 69 0.048690 3.359623
2005-05-29 733 0 -1 0.045404 -0.045404
2005-05-02 760 0 -1 0.039679 -0.039679
2005-04-02 790 0 -1 0.034160 -0.034160
2005-03-13 810 0 -1 0.030915 -0.030915
2004-11-09 934 0 -1 0.016647 -0.016647
我需要删除 line_race
等于 0
的行。做这件事的最有效方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果我正确理解,那应该很简单:
If I'm understanding correctly, it should be as simple as:
但是对于任何将来的旁路人,您都可以提到
df = df [df.line_race!= 0]
在尝试过滤none
/缺失值时,无需做任何事情。确实有效:
什么也不做:
有效:
But for any future bypassers you could mention that
df = df[df.line_race != 0]
doesn't do anything when trying to filter forNone
/missing values.Does work:
Doesn't do anything:
Does work:
只是为了添加另一个解决方案,如果您使用新的大熊猫评估师,其他解决方案将取代原始熊猫并丢失评估者
just to add another solution, particularly useful if you are using the new pandas assessors, other solutions will replace the original pandas and lose the assessors
在多个值和str dtype 的情况下,
我使用以下来过滤col中的给定值:
示例:
在dataframe中,我想删除列中具有“ b”和“ c”的行“ str”
In case of multiple values and str dtype
I used the following to filter out given values in a col:
Example:
In a DataFrame I want to remove rows which have values "b" and "c" in column "str"
如果要根据列的多个值删除行,则可以使用:
以
line_race
为0和10的所有行。If you want to delete rows based on multiple values of the column, you could use:
To drop all rows with values 0 and 10 for
line_race
.尽管以前的答案几乎与我要做的事情相似,但是使用索引方法不需要使用其他索引方法.loc()。可以以类似但精确的方式完成
Though the previous answer are almost similar to what I am going to do, but using the index method does not require using another indexing method .loc(). It can be done in a similar but precise manner as
最好的方法是使用布尔屏蔽:
更新:现在pandas 0.13出来了,另一种做到这一点的方法是
df.query('line_race!= 0')。
The best way to do this is with boolean masking:
UPDATE: Now that pandas 0.13 is out, another way to do this is
df.query('line_race != 0')
.但是,给定的答案是正确的,但是上面的人说您可以使用
df.Query('line_race!= 0')
哪些取决于您的问题要快得多。强烈推荐。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. Highly recommend.有多种方法可以实现这一目标。根据一个人的用例的特殊性,将留下以下各种选择,可以使用。
人们会认为OP的数据帧存储在变量
df
中。选项1
情况
对于OP的 并非总是如此,建议检查以下选项,其中指定列名。
选项2
tshauck的方法最终比选项1更好,因为一个人能够指定柱子。但是,还有其他变化,具体取决于一个人要参考该列的方式:
例如,使用数据框中的位置
或明确指示列以下的列,
也可以遵循相同的登录,但使用自定义的lambda函数,这样AS
选项3
使用
pandas 。
dataframe.drop.html“ rel =“ noreferrer”>
pandas.dataframe.drop
如下所示选项5
使用
强>
使用和
pandas.dataframe.query.query
如下所示
选项7
如果对输出没有强烈的意见,则可以使用
转换为dataframe
这也可以通过最有效的解决方案 这将取决于人们想要如何衡量效率。假设一个人想衡量执行时间,那么一种可以执行此操作的方式是
time.perf_counter()
。如果一个人测量上述所有选项的执行时间,则获得以下
但是,这可能会根据一个人使用的数据框架(例如硬件)等而改变。
注意:
使用
inplace = true
有各种建议。建议阅读以下内容: https://stackoverflow.com/a/59242208/7109869.apply()
有很强的意见。会建议阅读以下内容:我什么时候应该(不)在我的代码中使用pandas apply()?一个人缺少值,也许要考虑
pandas.dataframe.dropna
。使用选项2,就像
还有其他方法可以测量执行时间,因此我建议使用此线程:如何获得Python程序执行的时间?
There are various ways to achieve that. Will leave below various options, that one can use, depending on specificities of one's use case.
One will consider that OP's dataframe is stored in the variable
df
.Option 1
For OP's case, considering that the only column with values
0
is theline_race
, the following will do the workHowever, as that is not always the case, would recommend checking the following options where one will specify the column name.
Option 2
tshauck's approach ends up being better than Option 1, because one is able to specify the column. There are, however, additional variations depending on how one wants to refer to the column:
For example, using the position in the dataframe
Or by explicitly indicating the column as follows
One can also follow the same login but using a custom lambda function, such as
Option 3
Using
pandas.Series.map
and a custom lambda functionOption 4
Using
pandas.DataFrame.drop
as followsOption 5
Using
pandas.DataFrame.query
as followsOption 6
Using
pandas.DataFrame.drop
andpandas.DataFrame.query
as followsOption 7
If one doesn't have strong opinions on the output, one can use a vectorized approach with
numpy.select
This can also be converted to a dataframe with
With regards to the most efficient solution, that would depend on how one wants to measure efficiency. Assuming that one wants to measure the time of execution, one way that one can go about doing it is with
time.perf_counter()
.If one measures the time of execution for all the options above, one gets the following
However, this might change depending on the dataframe one uses, on the requirements (such as hardware), and more.
Notes:
There are various suggestions on using
inplace=True
. Would suggest reading this: https://stackoverflow.com/a/59242208/7109869There are also some people with strong opinions on
.apply()
. Would suggest reading this: When should I (not) want to use pandas apply() in my code?If one has missing values, one might want to consider as well
pandas.DataFrame.dropna
. Using the option 2, it would be something likeThere are additional ways to measure the time of execution, so I would recommend this thread: How do I get time of a Python program's execution?
高效且熊猫的方法之一是使用
eq()
方法:One of the efficient and pandaic way is using
eq()
method:另一种做到这一点的方式。可能不是最有效的方法,因为代码看起来比其他答案中提到的代码更为复杂,但仍然可以替代执行同一操作的方法。
Another way of doing it. May not be the most efficient way as the code looks a bit more complex than the code mentioned in other answers, but still alternate way of doing the same thing.
我编译并运行代码。这是准确的代码。您可以自己尝试。
您在列名中有任何特殊字符或空间
如果
角色您可以直接访问它。
I compiled and run my code. This is accurate code. You can try it your own.
If you have any special character or space in column name you can write it in
''
like in the given code:If there is just a single string column name without any space or special
character you can directly access it.
提供了很多选项(或者我没有太多关注,对不起,如果是这样),但是没有人提到这一点:
我们可以在熊猫中使用此符号:
so many options provided(or maybe i didnt pay much attention to it, sorry if its the case), but no one mentioned this:
we can use this notation in pandas: ~ (this gives us the inverse of the condition)
只需在所有列上添加另一种方法以扩展数据框:
示例:
Just adding another way for DataFrame expanded over all columns:
Example:
以防万一您需要删除行,但值可以在不同的列中。
就我而言,我正在使用百分比,因此我想删除任何列中具有值1的行,因为这意味着
如果您的DF有太多列,则100%不是最佳的。
Just in case you need to delete the row, but the value can be in different columns.
In my case I was using percentages so I wanted to delete the rows which has a value 1 in any column, since that means that it's the 100%
Is not optimal if your df have too many columns.
您可以尝试使用此信息:
。
You can try using this:
.
如果您需要根据索引值删除行,则最高答案中的布尔索引也可以进行调整。例如,在以下代码中,删除索引在3和7之间的行。
正如其他人提到的那样,
query()
是一个非常可读的功能,非常适合此任务。实际上,对于大型数据框,它是该任务的最快方法(请参见此答案有关基准结果)。一些带有
query()
的常见问题:@
的前缀为前缀。If you need to remove rows based on index values, the boolean indexing in the top answer may be adapted as well. For example, in the following code, rows where the index is between 3 and 7 are removed.
As others have mentioned,
query()
is a very readable function that is perfect for this task. In fact, for large dataframes, it is the fastest method for this task (see this answer for benchmark results).Some common questions with
query()
:@
.该线程中有几个涉及索引的答案,如果索引具有重复项,则大多数答案将无效。是的,这已经在上面的至少一条评论中指出,并且还指出,重新索引是解决这个问题的一种方式。这是一个重复索引以说明问题的示例。
这是输出:
There are several answers in this thread involving the index, and most of those answers will not work if the index has duplicates. And yes, that has been pointed out in at least one of the comments above, and it has also been pointed out that re-indexing is a way around this issue. Here is an example with a repeated index to illustrate the issue.
This is the output:
使用
.loc
不使用.drop
,您可以使用:Using
.loc
without using.drop
, you could use :对于这样的简单示例,它并没有太大的不同,但是对于复杂的逻辑,我更喜欢在删除行时使用
drop()
,因为它比使用倒数逻辑更简单。例如,删除A = 1和(B = 2或C = 3)
的行。这是一种易于理解并可以处理复杂逻辑的可扩展语法:
It doesn't make much difference for simple example like this, but for complicated logic, I prefer to use
drop()
when deleting rows because it is more straightforward than using inverse logic. For example, delete rows whereA=1 AND (B=2 OR C=3)
.Here's a scalable syntax that is easy to understand and can handle complicated logic: