系列的真值不明确-调用函数时出错 [英] The truth value of a Series is ambiguous - Error when calling a function
问题描述
我知道以下错误
ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
<很久以前有人问过。
has been asked a long time ago.
但是,我试图创建一个基本函数并使用 df ['busy']
返回一个新列 1
或 0
。我的函数看起来像这样,
However, I am trying to create a basic function and return a new column with df['busy']
with 1
or 0
. My function looks like this,
def hour_bus(df):
if df[(df['hour'] >= '14:00:00') & (df['hour'] <= '23:00:00')&\
(df['week_day'] != 'Saturday') & (df['week_day'] != 'Sunday')]:
return df['busy'] == 1
else:
return df['busy'] == 0
我可以执行该函数,但是当我用DataFrame调用它时,出现了上面提到的错误。我遵循以下线程和另一个线程创建该功能。我在 if
子句中使用了&
而不是和
。
I can execute the function, but when I call it with the DataFrame, I get the error mentioned above. I followed the following thread and another thread to create that function. I used &
instead of and
in my if
clause.
无论如何,当我执行以下操作时,我会得到所需的输出。
Anyhow, when I do the following, I get my desired output.
df['busy'] = np.where((df['hour'] >= '14:00:00') & (df['hour'] <= '23:00:00') & \
(df['week_day'] != 'Saturday') & (df['week_day'] != 'Sunday'),'1','0')
关于我在 hour_bus
中犯什么错误的任何想法
Any ideas on what mistake am I making in my hour_bus
function?
推荐答案
The
(df['hour'] >= '14:00:00') & (df['hour'] <= '23:00:00')& (df['week_day'] != 'Saturday') & (df['week_day'] != 'Sunday')
给出一个布尔数组,当您为您的 df
编制索引,您将获得(可能)较小的 df
部分。
gives a boolean array, and when you index your df
with that you'll get a (probably) smaller part of your df
.
仅说明我的意思:
import pandas as pd
df = pd.DataFrame({'a': [1,2,3,4]})
mask = df['a'] > 2
print(mask)
# 0 False
# 1 False
# 2 True
# 3 True
# Name: a, dtype: bool
indexed_df = df[mask]
print(indexed_df)
# a
# 2 3
# 3 4
但是它仍然是 DataFrame
,因此将其用作需要真值的表达式是模棱两可的(在您的 if
)。
However it's still a DataFrame
so it's ambiguous to use it as expression that requires a truth value (in your case an if
).
bool(indexed_df)
# ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
您可以使用 np.where
二手-或等效方式:
You could use the np.where
you used - or equivalently:
def hour_bus(df):
mask = (df['hour'] >= '14:00:00') & (df['hour'] <= '23:00:00')& (df['week_day'] != 'Saturday') & (df['week_day'] != 'Sunday')
res = df['busy'] == 0
res[mask] = (df['busy'] == 1)[mask] # replace the values where the mask is True
return res
但是 np.where
是更好的解决方案(可读性更好,而且速度可能更快)。
However the np.where
will be the better solution (it's more readable and probably faster).
这篇关于系列的真值不明确-调用函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!