1

I had a question where i needed to find how many people have the word "Chief" in their jobtitle? i was using the below two approaches but both of them give different answers, kindly let me know what's the difference and which should i use.

Approach 1:

len(sal[sal.JobTitle.str.contains("Chief", case=True, na=False)])

This gives output as 423

Approach 2:

count = 0
for job in sal['JobTitle']:
    for j in job.split():
        if j.lower() =='chief':
            count+=1
            print(count)

This gives the correct output 477.

1
  • Use case=False in the first code. Also, you might have rows with multiple occurrences of "chief" in the same string, you count them in the second approach, not the first Commented Jul 28, 2022 at 5:15

1 Answer 1

2

It is because of your approach 1 count 'Chief' but approach 2 first lower and thencount 'chief'. It is possible to there were some records with lowercase in job title and approach 1 can not count them

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.