删除Pandas数据帧中基于整行最大值的行。

huangapple go评论54阅读模式
英文:

How do I drop rows from a Pandas dataframe based on the maximum value of the whole row?

问题

我想根据行中所有值的最大值的条件来删除行,而不是像该站点上大多数解决方案一样逐列进行操作。

假设我有以下数据框:

AA BB
A 5 8
B 11 55

我想要删除所有行,其中行的最大值 <= 50。所以行A应该被删除,因为该行中的最大值只有8。

我该如何操作?

英文:

I would like to drop rows based on a conditional of the maximum of all the values in the row, not on a column by column basis like most of the solutions on this site demonstrate.

Let's say I have the following dataframe:

AA BB
A 5 8
B 11 55

I want to drop all the rows where the maximum value of the row is <= 50. So row A should be dropped since the max value in the row is only 8.
How do I go about doing this?

答案1

得分: 2

与其删除行,不如保留正确的行:

# 使用最大值筛选
df[df.max(axis=1).gt(50)]

# 或者参考 @BigBen 的建议
df[df.gt(50).any(axis=1)]
英文:

Instead of dropping rows, you can keep right ones:

&gt;&gt;&gt; df[df.max(axis=1).gt(50)]
    AA   BB
B    11  55

# Or suggested by @BigBen
&gt;&gt;&gt; df[df.gt(50).any(axis=1)]
    AA   BB
B    11  55

答案2

得分: 2

使用drop Pandas函数,例如:

import pandas as pd

df = pd.DataFrame({'AA':[5, 11],
                   'BB':[8, 55],
                   })


df = df.drop(df[df.max(axis=1) < 50].index)

# &gt;&gt;&gt; df
#    AA  BB
# 1  11  55
英文:

With drop Pandas function for instance :

import pandas as pd

df = pd.DataFrame({&#39;AA&#39;:[5, 11],
                   &#39;BB&#39;:[8, 55],
                   })


df = df.drop(df[df.max(axis=1) &lt; 50].index)

# &gt;&gt;&gt; df
#    AA  BB
# 1  11  55

huangapple
  • 本文由 发表于 2023年2月7日 05:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定