熊猫在数据框上的比较
当我对数据框中的单个元素进行比较时出现错误,但我不明白为什么。
我有一个数据框df,其中包含许多客户的时间序列数据,其中包含一些空值:
df.head()
8143511 8145987 8145997 8146001 8146235 8147611 \
2012-07-01 00:00:00 NaN NaN NaN NaN NaN NaN
2012-07-01 00:30:00 0.089 NaN 0.281 0.126 0.190 0.500
2012-07-01 01:00:00 0.090 NaN 0.323 0.141 0.135 0.453
2012-07-01 01:30:00 0.061 NaN 0.278 0.097 0.093 0.424
2012-07-01 02:00:00 0.052 NaN 0.278 0.158 0.170 0.462
在我的脚本中,该行 if pd.isnull(df[[customer_ID]].loc[ts]):
生成一个错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(),
a.item(), a.any() or a.all().
但是,如果我在脚本行上放置了一个断点,并且在脚本停止时我将其输入到控制台中:
pd.isnull(df[[customer_ID]].loc[ts])
输出为:
8143511 True
Name: 2012-07-01 00:00:00, dtype: bool
如果我允许脚本从这一点继续执行,则会立即生成错误。
如果布尔表达式可以求值并具有值True
,为什么它在if表达式中生成错误?这对我来说毫无意义。