[Tutor] Question about using series.str.startswith() in pandas series

Manprit Singh manpritsinghece at gmail.com
Thu Sep 3 08:37:27 EDT 2020


Dear sir ,

Consider a dataframe given below

    Subject          Name
0      Arts   Miss. Romie
1   Science     Mr. Peter
2  Commerce   Mrs. Serena
3      Arts  Master James
4   Science  Miss. Nickey

Now from the Name column of the dataframe i have to count the number of males

and females . Name starting with Miss. or Mrs. is of a female and the name

which starts with Mr. or Master is of a male. the above mentioned dataframe

is assigned to a variable df.

Now if i write a line of code like this :

df.iloc[:, 1].str.startswith("Miss.").sum()

The output of the code will be 2, and i am getting the same answer, which is

correct as there are only 2 names in the dataframe that startswith Miss.

Now coming to the point, if i have to count the number of females in the

dataframe, i can count the names that starts with Miss. or Mrs.

for that if i write a code like this, will it be valid to use:

df.iloc[:, 1].str.startswith(("Miss.", "Mrs.")).sum()

The code is giving the right answer, which is 3

I am asking this question because, in official pandas documentation there is

not a single example, in which a tuple of multiple items is being passed

as an argument to the pandas series.str.startswith() method.You can clearly

see in the above example that i have passed a tuple ("Miss.", "Mrs.") to

the series.str.startswith()

Need help

Regards

Manprit singh


More information about the Tutor mailing list