Using filter in dataframe

Question:

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.

Asked By: Vikas Yadav

||

Answers:

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

Answered By: Alireza