0

This is my code. i want to return

  1. 1 if 0 <= x <= 13
  2. 2 if 14 <= x <= 26
  3. 3 if 27 <= x <= 40
def get_part_b_values():
    x = (dataset['part_b_q1'].apply(part_b_values) 
        + dataset['part_b_q2'].apply(part_b_values) 
        + dataset['part_b_q3'].apply(part_b_values)  
        + dataset['part_b_q4'].apply(part_b_values_reverse) 
        + dataset['part_b_q5'].apply(part_b_values_reverse) 
        + dataset['part_b_q6'].apply(part_b_values) 
        + dataset['part_b_q7'].apply(part_b_values_reverse) 
        + dataset['part_b_q8'].apply(part_b_values_reverse) 
        + dataset['part_b_q9'].apply(part_b_values) 
        + dataset['part_b_q10'].apply(part_b_values))
    if ((x>=0) & (x<=13)):
        return 1
    elif((x>=14) & (x<=26)):
        return 2
    else:
        return 3

but when im running this code i got this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/ym/_wjy7pyj6zjdlnf94rq4grjr0000gn/T/ipykernel_1155/3542724477.py in <module>
----> 1 dataset['newCol'] = test()

/var/folders/ym/_wjy7pyj6zjdlnf94rq4grjr0000gn/T/ipykernel_1155/2427638497.py in test()
      1 def test():
----> 2     t = get_part_b_values()
      3     if (t.between(0,13)):
      4         return 1
      5     elif (t.between(14,27)):

/var/folders/ym/_wjy7pyj6zjdlnf94rq4grjr0000gn/T/ipykernel_1155/2029802580.py in get_part_b_values()
     12         + dataset['part_b_q9'].apply(part_b_values)
     13         + dataset['part_b_q10'].apply(part_b_values))
---> 14     if ((x>=0) & (x<=13)):
     15         return 1
     16     elif((x>=14) & (x<=26)):

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py in __nonzero__(self)
   1525     @final
   1526     def __nonzero__(self):
-> 1527         raise ValueError(
   1528             f"The truth value of a {type(self).__name__} is ambiguous. "
   1529             "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().

i tried to google. but i couldnt find a solution for this.

  • 2
    `.apply` returns a [Series or DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html), so when you perform `x = __ + __ + ...` x is a concatenated Series or DataFrame; performing `>=` on such a type results in the `ValueError` you are seeing. Why do you expect `x` to be a numerical type? What do you expect `x = __.apply + __.apply + ...` is doing for you? – A.J. Uppal Jan 03 '23 at 21:35
  • try `print(x)` before your `if` statement. it's probably a pandas Series instead of an integer. otherwise it's hard to help not knowing what `part_b_values` is – mitoRibo Jan 03 '23 at 21:35

0 Answers0