Pandas Boolean.any (). All ()

I kept getting ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). when trying boolean tests using pandas. Not understanding what he said, I decided to try to understand this.

However, I am completely confused at this point.

Here I create a dataframe of two variables with a single data point shared between them (3):

 In [75]: import pandas as pd df = pd.DataFrame() df['x'] = [1,2,3] df['y'] = [3,4,5] 

Now I try everything (x less than y), which I translated to "all values ​​x less than y", and I get an answer that does not make sense.

 In [79]: if all(df['x'] < df['y']): print('True') else: print('False') True 

Next I will try any (is x less than y), which I will outweigh by β€œany value of x less than y”, and I get another answer that does not make sense.

 In [77]: if any(df['x'] < df['y']): print('True') else: print('False') False 

In short: what does any () and all () actually do?

+6
source share
1 answer

Pandas suggests you use the Series any() and all() methods, rather than the Python built-in functions.

I don’t quite understand the source of the strange output you have (I get True in both cases in Python 2.7 and Pandas 0.17.0). But try the following, it should work. This uses the Series.any() and Series.all() methods.

 import pandas as pd df = pd.DataFrame() df['x'] = [1,2,3] df['y'] = [3,4,5] print (df['x'] < df['y']).all() # more pythonic way of print (df['x'] < df['y']).any() # doing the same thing 

This should print:

 True True 
+6
source

Source: https://habr.com/ru/post/980560/


All Articles