[Tutor] Question about using series.str.startswith() in pandas series
Deepak Dixit
deepakdixit0001 at gmail.com
Thu Sep 3 13:03:51 EDT 2020
If you will look to the Python 3 docs, multiple strings can be passed in
tuple.
Check it: https://docs.python.org/3/library/stdtypes.html#str.startswith
On Thu, Sep 3, 2020, 7:46 PM Manprit Singh <manpritsinghece at gmail.com>
wrote:
> 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
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list