Search code examples
pythonpandasbuilt-inany

Why do any() and pd.any() return different values?


I recently discovered that the built-in function any() doesn't work for pandas dataframes.

import pandas as pd
data = pd.DataFrame([True, False])

print("base: " + str(any(data)))
print("pandas: " + str(data.any()))

Result:

base: False
pandas: 0    True
dtype: bool

Can someone explain the logic behind this behavior?


Solution

  • Iterating through a dataframe is iterating through its column labels, e. g.

    In[3]: df = pd.DataFrame({"col_1": [1, 2], "col_2": [3, 4]})
    In[4]: df
    
       col_1  col_2
    0      1      3
    1      2      4
    
    In[5]: for i in df:
      ...:     print(i)
    
    col_1
    col_2
    

    In your case with only 1 column with the default label 0 (it is the number 0, not a string '0'), you obtained for

    any(data),

    which is as

    any([0]),

    which in turn is as

    any([False])

    the value False.