Search code examples
pythondata-sciencedata-analysis

Error getting when run code in Python "The truth value of a Series is ambiguous Use a.empty, a.bool(), a.item(), a.any() or a.all().."


When i run below code in Python

cols_with_missing = (col for col in new_data.columns if new_data[col].isnull().any())
for col in cols_with_missing:
    new_data[col + '_was_missing'] = new_data[col].isnull()

then getting below error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Here new_data is a DataFrame.


Solution

  • The problem is that Series.any() returns a new Series, as stated in the documentation.

    To obtain a single value (a scalar), you need to pass None as axis argument.

    cols_with_missing = (col for col in new_data.columns if new_data[col].isnull().any(None))