Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

python 2.7 - ValueError: The truth value of a Series is ambiguous

>>> df.head()
                         № Summer  Gold  Silver  Bronze  Total  № Winter  
Afghanistan (AFG)              13     0       0       2      2         0
Algeria (ALG)                  12     5       2       8     15         3
Argentina (ARG)                23    18      24      28     70        18
Armenia (ARM)                   5     1       2       9     12         6
Australasia (ANZ) [ANZ]         2     3       4       5     12         0

                         Gold.1  Silver.1  Bronze.1  Total.1  № Games  Gold.2  
Afghanistan (AFG)             0         0         0        0       13       0
Algeria (ALG)                 0         0         0        0       15       5
Argentina (ARG)               0         0         0        0       41      18
Armenia (ARM)                 0         0         0        0       11       1
Australasia (ANZ) [ANZ]       0         0         0        0        2       3

                         Silver.2  Bronze.2  Combined total
Afghanistan (AFG)               0         2               2
Algeria (ALG)                   2         8              15
Argentina (ARG)                24        28              70
Armenia (ARM)                   2         9              12
Australasia (ANZ) [ANZ]         4         5              12

Not sure why do I see this error:

>>> df['Gold'] > 0  | df['Gold.1'] > 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ankuragarwal/data_insight/env/lib/python2.7/site-packages/pandas/core/generic.py", line 917, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Whats ambiguous here ?

But this works:

>>> (df['Gold'] > 0)  | (df['Gold.1'] > 0)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming we have the following DF:

In [35]: df
Out[35]:
   a  b  c
0  9  0  1
1  7  7  4
2  1  8  9
3  6  7  5
4  1  4  6

The following command:

df.a > 5 | df.b > 5

because | has higher precedence (compared to >) as it's specified in the Operator precedence table) it will be translated to:

df.a > (5 | df.b) > 5

which will be translated to:

df.a > (5 | df.b) and (5 | df.b) > 5

step by step:

In [36]: x = (5 | df.b)

In [37]: x
Out[37]:
0     5
1     7
2    13
3     7
4     5
Name: c, dtype: int32

In [38]: df.a > x
Out[38]:
0     True
1    False
2    False
3    False
4    False
dtype: bool

In [39]: x > 5
Out[39]:
0    False
1     True
2     True
3     True
4    False
Name: b, dtype: bool

but the last operation won't work:

In [40]: (df.a > x) and (x > 5)
---------------------------------------------------------------------------
...
skipped
...
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The error message above might lead inexperienced users to do something like this:

In [12]: (df.a > 5).all() | (df.b > 5).all()
Out[12]: False

In [13]: df[(df.a > 5).all() | (df.b > 5).all()]
...
skipped
...
KeyError: False

But in this case you just need to set your precedence explicitly in order to get expected result:

In [10]: (df.a > 5) | (df.b > 5)
Out[10]:
0     True
1     True
2     True
3     True
4    False
dtype: bool

In [11]: df[(df.a > 5) | (df.b > 5)]
Out[11]:
   a  b  c
0  9  0  1
1  7  7  4
2  1  8  9
3  6  7  5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...