Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 607
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 2
Kenil Vasani
Asked: December 14, 20202020-12-14T21:06:57+00:00 2020-12-14T21:06:57+00:00In: Python

Error: The truth value of a Series is ambiguous – Python pandas

  • 2

I know this question has been asked before, however, when I am trying to do an if statement and I am getting an error. I looked at this link , but did not help much in my case. My dfs is a list of DataFrames.

I am trying the following,

for i in dfs:
    if (i['var1'] < 3.000):
       print(i)

Gives the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

AND I tried the following and getting the same error.

for i,j in enumerate(dfs):
    if (j['var1'] < 3.000):
       print(i)

My var1 data type is float32. I am not using any other logical operators and & or |. In the above link it seemed to be because of using logical operators. Why do I get ValueError?

logicpandaspython
  • 1 1 Answer
  • 9 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      Here is a small demo, which shows why this is happenning:

      In [131]: df = pd.DataFrame(np.random.randint(0,20,(5,2)), columns=list('AB'))
      
      In [132]: df
      Out[132]:
          A   B
      0   3  11
      1   0  16
      2  16   1
      3   2  11
      4  18  15
      
      In [133]: res = df['A'] > 10
      
      In [134]: res
      Out[134]:
      0    False
      1    False
      2     True
      3    False
      4     True
      Name: A, dtype: bool
      

      when we try to check whether such Series is True – Pandas doesn’t know what to do:

      In [135]: if res:
           ...:     print(df)
           ...:
      ---------------------------------------------------------------------------
      ValueError                                Traceback (most recent call last)
      ...
      skipped
      ...
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      

      Workarounds:

      we can decide how to treat Series of boolean values – for example if should return True if all values are True:

      In [136]: res.all()
      Out[136]: False
      

      or when at least one value is True:

      In [137]: res.any()
      Out[137]: True
      
      In [138]: if res.any():
           ...:     print(df)
           ...:
          A   B
      0   3  11
      1   0  16
      2  16   1
      3   2  11
      4  18  15
      
      • 1
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      Error: PostCSS plugin tailwindcss requires PostCSS 8

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com