英文:
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:
>>> df[df.max(axis=1).gt(50)]
AA BB
B 11 55
# Or suggested by @BigBen
>>> 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)
# >>> df
# AA BB
# 1 11 55
英文:
With drop
Pandas function for instance :
import pandas as pd
df = pd.DataFrame({'AA':[5, 11],
'BB':[8, 55],
})
df = df.drop(df[df.max(axis=1) < 50].index)
# >>> df
# AA BB
# 1 11 55
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论