2017-04-05 77 views
1

我收到错误时,我对数据框中的单个元素进行比较,但我不明白为什么。熊猫布尔comparisson对数据框

我有时间序列数据的数据帧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表达式中生成一个错误?这对我来说没有意义。

+0

检查答案:http://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty -a-bool-a-item-a-any-o – Rohanil

+0

好的,谢谢。因此,如果我使用 pd.isnull(df_gen [[customer_ID]]。loc [ts] .item()),那么该布尔值被评估为OK,但我不明白为什么原始文件不起作用。 – doctorer

+0

由于原始返回''对象,它不是布尔值。 – Rohanil

回答

0

第二组[]正在返回一个我误认为单个值的系列。最简单的解决方法是删除[]:这

if pd.isnull(df[customer_ID].loc[ts]): 
     pass 
2

问题是你需要返回标(TrueFalse)比较标量,但有一个项目Series,将其转化为一个项目boolean Series

解决方案是采用Series.itemvalues与选择第一个值转换为标量由[0]

customer_ID = '8143511' 
ts = '2012-07-01 00:00:00' 

print (df[[customer_ID]].loc[ts].item()) 
nan 

if pd.isnull(df[[customer_ID]].loc[ts]).item(): 
    print ('super') 
print (df[[customer_ID]].loc[ts].values[0]) 
nan 

if pd.isnull(df[[customer_ID]].loc[ts]).values[0]: 
    print ('super') 

但如果使用DataFrame.loc,得到scalar(如果不重复索引或列名):

print (df.loc[ts, customer_ID]) 
nan 

customer_ID = '8143511' 
ts = '2012-07-01 00:00:00' 
if pd.isnull(df.loc[ts, customer_ID]): 
    print ('super') 
+0

谢谢。这让我意识到第二套'[]'是造成这个问题的原因,所以这些解决方案的工作原理很简单,但是移除方括号就更简单了。 – doctorer

+0

很高兴能为您提供帮助。祝你好运! – jezrael

4

问题李es在if声明中。

当你的代码

if this: 
    print(that) 

thisbool(this)进行评估。而且更好的回来为TrueFalse

但是,你这样做:

if pd.isnull(df[[customer_ID]].loc[ts]): 
    pass # idk what you did here because you didn't say... but doesn't matter 

而且,你说pd.isnull(df[[customer_ID]].loc[ts])评价:

8143511 True 
Name: 2012-07-01 00:00:00, dtype: bool 

不会看起来像TrueFalse
bool(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.Series不能评价然而作为TrueFalse

是,,的True S和Falsepd.Series

而这就是为什么它不起作用。

+0

实际上,我学到的教训是'df [[customer_ID]]。loc [ts]'返回一个'pd.Series'不是一个单独的值 – doctorer