Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

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

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/What is the best solution for: ''the truth value of a series is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().'' error?
Next
Answered
domenicalindgren
  • 8
domenicalindgren
Asked: June 8, 20222022-06-08T16:02:41+00:00 2022-06-08T16:02:41+00:00In: Error

What is the best solution for: ”the truth value of a series is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().” error?

  • 8

. Advertisement .

..3..

. Advertisement .

..4..

Hi everyone. I’m learning about pandas DataFrame and I’m in trouble with ”the truth value of a series is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().” error. Here is the detail of program which I run:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'points': [18, 22, 19, 14, 14, 11, 20, 28],
'assists': [5, 7, 7, 9, 12, 9, 9, 4],
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

team points assists rebounds
0 A 18 5 11
1 A 22 7 8
2 A 19 7 10
3 A 14 9 6
4 B 14 12 6
5 B 11 9 5
6 B 20 9 9
7 B 28 4 12

I receive this error when I try to filter for rows where the team is equal “A” and the points are less than 20:

#attempt to filter DataFrame

df[(df['team'] == 'A') and (df['points'] < 20)]

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

And when we I try to filter for rows where the team is equal to “A” or the points is less than 20, I get following error:

#attempt to filter DataFrame
df[(df['team'] == 'A') or (df['points'] < 20)]

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

I have read a lot of documents but I haven’t found the desired answer. Can you help me with this problem? Please write your suggestions below. Thanks!

the truth value of a series is ambiguous
  • 1 1 Answer
  • 82 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-08T16:39:47+00:00Added an answer on June 8, 2022 at 4:39 pm

    The cause:

    This error happens because instead of using the & and | operators, you try to filter a pandas DataFrame with the words and and or.

    Solution:

    To solve this error, you have to use  the & and | instead of the words and and or.

    Below are the codes I suggest:

    #filter DataFrame
    df[(df['team'] == 'A') & (df['points'] < 20)]
    
    team points assists rebounds
    0 A 18 5 11
    2 A 19 7 10
    3 A 14 9 6
    #filter DataFrame
    df[(df['team'] == 'A') | (df['points'] < 20)]
    
    team points assists rebounds
    0 A 18 5 11
    1 A 22 7 8
    2 A 19 7 10
    3 A 14 9 6
    4 B 14 12 6
    5 B 11 9 5
    • 8
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.