【发布时间】: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