具有布尔逻辑的含糊的真值 [英] Ambiguous truth value with boolean logic
问题描述
我试图在数据框中的一个函数中使用一些布尔逻辑,但会出现错误:
在[4]中:
data = {'level':[20,19,20,21,25,29,30,31,30,29,31]}
框架= DataFrame(数据)
框架
出[4]:
级
0 20
1 19
2 20
3 21
4 25
5 29
6 30
7 31
8 30
9 29
10 31
在[35]:
def calculate(x):
baseline = max(frame ['level'],frame ['level']。shift(1))#does not work
#baseline = x ['level'] + 4#works
difftobase = x ['level'] - 基线
返回基线,difftobase
框架['baseline'],框架['difftobase'] = zip(* frame.apply(calculate,axis = 1))#works
但是,这会在以下情况引发以下错误:
baseline = max(frame ['level'],frame ['level '] .shift(1))#does not work
pre>
ValueError:('系列的真值是不明确的,使用.empty,a.bool(),a.item(),a.any()或a.all()。',你在索引0'中出现)
我读了如何回顾Pandas数据框函数调用中的前一行?
和 http://pandas.pydata.org/pandas-docs/stable/gotchas.html
但不能弄清楚如何将其应用于我的问题?解决方案不足的使用函数max。 np.maximum(也许np.ma.max以及每个numpy文档)工作。显然,常规的max不能处理数组(容易)。替换
baseline = max(frame ['level'],frame ['level']。shift(1))#doesnt工作
与
baseline = np.maximum(frame ['level'],frame ['level']。shift(1))
做的伎俩。我删除了另一部分,使其更容易阅读:
在[23]中:
#q 1分析
def calculate_rowise(x):
baseline = np.maximum(frame ['level'],frame ['level']。shift(1))#works
return baseline
frame.apply(calculate_rowise)
输出[23]:
级别
0 NaN
1 20
2 20
3 21
4 25
5 29
6 30
7 31
8 31
9 30
10 31
PS原始问题是隐藏在取出功能的shift部分时出现的另一个问题。返回形状不匹配,但这又是一个问题,只是在这里提及,以获得充分的披露。
I am trying to use some boolean logic in a function on a dataframe, but get an error:
In [4]:
data={'level':[20,19,20,21,25,29,30,31,30,29,31]} frame=DataFrame(data) frame Out[4]: level 0 20 1 19 2 20 3 21 4 25 5 29 6 30 7 31 8 30 9 29 10 31 In [35]: def calculate(x): baseline=max(frame['level'],frame['level'].shift(1))#doesnt work #baseline=x['level']+4#works difftobase=x['level']-baseline return baseline, difftobase frame['baseline'], frame['difftobase'] = zip(*frame.apply(calculate, axis=1))#works
However, this throws the following error at:
baseline=max(frame['level'],frame['level'].shift(1))#doesnt work ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 0')
I read How to look back at previous rows from within Pandas dataframe function call? and http://pandas.pydata.org/pandas-docs/stable/gotchas.html but can't figure out how to apply this to my problem?
解决方案Inadequate use of the function max. np.maximum (perhaps np.ma.max as well as per numpy documentation) works. Apparently regular max can not deal with arrays (easily). Replacing
baseline=max(frame['level'],frame['level'].shift(1))#doesnt work
with
baseline=np.maximum(frame['level'],frame['level'].shift(1))
does the trick. I removed the other part to make it easier to read:
In [23]: #q 1 analysis def calculate_rowise(x): baseline=np.maximum(frame['level'],frame['level'].shift(1))#works return baseline frame.apply(calculate_rowise) Out[23]: level 0 NaN 1 20 2 20 3 21 4 25 5 29 6 30 7 31 8 31 9 30 10 31
PS the original problem is hiding another issue that shows up when taking out the shift portion of the function. The return shape doesn't match, but thats another problem, just mentioning it here for full disclosure
这篇关于具有布尔逻辑的含糊的真值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!