【问题标题】:Conditional inequality statements on two columns of pandas DataFrame两列 pandas DataFrame 上的条件不等式语句
【发布时间】:2021-05-09 08:10:00
【问题描述】:

对于 pandas DataFrame df 的两列上的两个条件相等语句,我们通常会使用

df.loc[(df['col1'] == 0.9 & (df['col2'] == 1)]

如何对条件不等式语句做同样的事情?

df.loc[(df['col1'] > 0.9) & (df['col2'] > 1)]

上面给出了一个错误,我认为是因为浮动0.9

TypeError: '>' not supported between instances of 'method' and 'float'

更具体地说,当使用df.col1 代替df['col1'] 等时会出现错误

【问题讨论】:

  • 请与预期输出共享示例数据框
  • col1 是函数名吗?
  • 不,只是列名
  • df['col1'] > 0.9 是否给出相同的错误?
  • @develarist 我知道这已经有一段时间了,但是如果下面的答案是您需要的,请告诉我们。如果是这样,请接受它以“关闭问题”。希望它有所帮助!

标签: python pandas dataframe conditional-statements inequality


【解决方案1】:

我相信你只需要添加外括号。

而不是这个:

df.loc[(df['col1'] > 0.9) & (df['col2'] > 1)]

试试这个:

df.loc[((df['col1'] > 0.9) & (df['col2'] > 1))]

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2018-05-05
    • 2017-04-06
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多