【问题标题】:How to check a column for str value, determine if another column is less/greater than [x] return boolean in newly made column如何检查列的 str 值,确定另一列是否小于/大于 [x] 在新建列中返回布尔值
【发布时间】:2021-11-09 20:13:03
【问题描述】:

我有一个看起来像这样的数据框

product duration
tire change 01:16:51
oil change 05:06:00
tire change 02:03:04
oil change 06:23:14
oil change 03:40:27

我想创建一个基于 2 列返回布尔值的新列

product duration duration_bool
tire change 01:16:51 True
oil change 01:06:00 True
tire change 04:03:04 False
oil change 02:23:14 False
oil change 03:40:27 False

这是在数据帧上实际使用函数的正确方法吗?我无法理解这是否真的完成了我所追求的目标。

def sla_bool_checker(my_var):

    #check if product is a tire change, if it is, check if duration is under 4 hours and return the Boolean in the new column

    if df['product'] == 'tire change' :
        df['duration_bool'] = df['duration'] < pd.Timedelta(4, unit='h')

    #check if product is a oil change, if it is, check if duration is under 2 hours and return the Boolean

    elif df['product'] == 'oil change' :
        df['duration_bool'] < pd.Timedelta(2, unit='h')

我不知道我缺少什么,但这是代码错误。

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

【问题讨论】:

    标签: python dataframe if-statement filter boolean


    【解决方案1】:

    根据您的条件创建一个布尔数组并将其分配给新列。

    df['duration'] = df['duration'].apply(pd.Timedelta) # make sure duration has a dtype of Timedelta
    
    df['duration_bool'] = ((df['product'] == 'tire change') & (df['duration'] < pd.Timedelta(4, unit='h'))) | \
    ((df['product'] == 'oil change') & (df['duration'] < pd.Timedelta(2, unit='h')))
    
           product        duration  duration_bool
    0  tire change 0 days 01:16:51           True
    1   oil change 0 days 05:06:00          False
    2  tire change 0 days 02:03:04           True
    3   oil change 0 days 06:23:14          False
    4   oil change 0 days 03:40:27          False
    

    什么意思

    ((df['product'] == 'tire change') &amp; (df['duration'] &lt; pd.Timedelta(4, unit='h'))),其中产品等于换胎且持续时间少于 4 小时。

    |

    ((df['product'] == 'oil change') &amp; (df['duration'] &lt; pd.Timedelta(2, unit='h'))) 其中产品等于换油且持续时间少于 2 小时

    【讨论】:

      【解决方案2】:

      首先,您的两个示例中的durations 不匹配,这使得很难比较输入与输出结果。请下次检查。然后你可以使用:

      df.loc[df["product"] == "tire change", "duration_bool"] = pd.to_timedelta(df["duration"]) < pd.Timedelta(4, unit="h")
      df.loc[df["product"] == "oil change", "duration_bool"] = pd.to_timedelta(df["duration"]) < pd.Timedelta(2, unit="h")
      

      这直接将行duration_bool 的值设置为pd.Timedelta(...) 函数的结果,但pd.to_timedelta(...) 确保它是一个要比较的时间增量。 这让你:

      |    | product     | duration   | duration_bool   |
      |---:|:------------|:-----------|:----------------|
      |  0 | tire change | 01:16:51   | True            |
      |  1 | oil change  | 01:06:00   | True            |
      |  2 | tire change | 04:03:04   | False           |
      |  3 | oil change  | 02:23:14   | False           |
      |  4 | oil change  | 03:40:27   | False           |
      

      【讨论】:

        【解决方案3】:

        我发现我需要在我的def sla_bool_checker 中创建一个return 子句。然后需要使用apply 将返回值应用于我的数据框。我仍然无法确切地了解apply 的工作原理,但它确实有效,我希望我对任何需要的人有更深入的解释。

        我可能应该使用 np.where() (仍然不清楚如何使其工作)但@it_is_chris 的答案实际上对我也很有效! (谢谢克里斯)

        从那里我一直在研究,因为我真的很想找到一种方法来使用它的功能。可能不太理想,但我学到了很多。

        这是我使用的代码。

        def sla_bool_checker(my_var):
            #check if product is a tire change, if it is, check if duration is under 4 hours and return the Boolean in new column
            if my_var['product'] == 'tire change' :
                return my_var['duration'] < pd.Timedelta(4, unit='h')
            #check if product is an oil change, if it is, check if duration is under 24 hours and return the Boolean
            elif my_var['product'] == 'oil change' :
                return my_var['duration'] < pd.Timedelta(2, unit='h')
        

        然后我用了

        df['duration_bool'] = df.apply(sla_bool_checker, axis=1)     
        df
        

        导致

        product duration duration_bool
        0 tire change 01:16:51 True
        1 oil change 01:06:00 True
        2 tire change 04:03:04 False
        3 oil change 02:23:14 False
        4 oil change 03:40:27 False

        【讨论】:

        • 如果任何答案对您有所帮助或提供了新的见解,如果您给予支持,作者将非常感谢。或者接受答案。谢谢!
        猜你喜欢
        • 2019-09-08
        • 2023-01-14
        • 2021-12-14
        • 1970-01-01
        • 2019-05-26
        • 2021-12-29
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        相关资源
        最近更新 更多