1
import importlib
import logging
import os
import numpy as np
import pandas as pd

if df['moderate_activity'] == 1:
    print("Participates in Moderate Activity")

The log lists this after running my code:

self = 0      False
1      False
2       True
3      False
4      False
       ...  
578     True
579     True
580    False
581     True
582     True
Name: moderate_activity, Length: 583, dtype: bool

    def __nonzero__(self):
        raise ValueError(
>           f"The truth value of a {type(self).__name__} is ambiguous. "
            "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
        )
E       ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

The error references the first line, moderate_activity is binary. This is the first time I'm running into this error - I looked up a couple of solutions when trying to implement it in an if statement, such as changing if df['moderate_activity'] == 1: to if df[df['moderate_activity'] == 1]: but that didn't work. What could I do to fix this?

  • You are comparing your entire Series with one value. If you are looking for each individual item in your series you can use filtering or if you want to check if all the items are equal `1` you can use `a.all()` and so on – Mohammad Tehrani Sep 28 '22 at 22:44

2 Answers2

1

df['moderate_activity'] returns a Series. It then gets compared to 1, returning a Series of booleans, which python then attempts to cast into a boolean because that's what it does with anything you throw in an if statement. However, a Series cannot be cast into a singular boolean since it contains multiple values, so the error message gives you some suggestions of what you can do. If your DataFrame only has 1 row, you still have to index only that row with something like df.loc[0, 'moderate_activity'] to get it as a scalar.

Xnot
  • 171
  • 1
  • 3
0

You are comparing your an entire column (df['moderate_activity']) to a single value (1) which is ambiguous.

You can either use something like .any() to check if any element is 1 or .all() to check if all elements are equal to 1.

Or you can use numpy.where to get the index of every element that is 1

import numpy as np
np.where(df['moderate_activity'] == 0)

To give you the best suggestion, you'll have to tell us a little bit more about what you want to achieve.

bitflip
  • 3,436
  • 1
  • 3
  • 22