【问题标题】:How to select all rows which contain values *in selected columns* greater than a threshold?如何选择所有包含*在选定列中*大于阈值的值的行?
【发布时间】:2020-05-26 21:47:44
【问题描述】:

我正在尝试做与this question 相同的事情,但我有一个字符串类型的列,我需要将其保留在数据框中,以便我可以识别哪些行是哪些行。 (我想我可以通过索引来做到这一点,但我希望能够节省一个步骤。)有没有办法在使用 .any() 时不计算列,但将其保留在结果数据框中?谢谢!

这是所有列上的代码:

df[(df > threshold).any(axis=1)]

这是我现在正在使用的硬编码版本:

df[(df[list_of__selected_columns] > 3).any(axis=1)]

这对我来说似乎有点笨拙,所以我想知道是否有更好的方法。

【问题讨论】:

标签: python pandas filter any


【解决方案1】:

您可以使用.select_dtype 选择所有列,例如数字列:

df[df.select_dtype(include='number').gt(threshold).any(axis=1)]

或一大块带有iloc的连续列:

df[df.iloc[:,3:6].gt(threshold).any(axis=1)]

如果你想选择一些随机的列列表,你最好通过硬编码列表来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2021-11-21
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多