Pandas endswith()
— this is another method for finding and filtering text data in a Series or endswith()
data. This method is similar to the Python method
Example # 1: Return of the Bool Series
In this example, the college column is checked if items have an "e" at the end of the line using str.endswith()
. A logical series is returned that evaluates to true at the index position where "e" appears at the end of the line. str.lower ()
is called before endwith (), since the data could be anyway.
|
Output:
As shown in the output image, the bool series is True at an index position where the College column ends with "e". This can also be compared by looking at the original dataframe image.
Example # 2 Handling NULL Values
The most important part of data analysis is handling null values. As you can see in the above output image, the boolean run has NaN where the value in the College column was blank or NaN. If this logical row is passed into a data frame, it will throw an error. Therefore, NaN values must be processed using the na parameter. It can also be set to a string, but since the bool series is used to pass and return the appropriate value, it should only be set to Bool.
In this example, na is False. Therefore, where the College column is Null, the Bool series will store False instead of NaN. The series is then passed back to the data frame to display only the true values.
|
Output:
As shown in the output image, there are lines in the data frame that have "e" at the end rows in the College column. NaN values are not displayed because the na parameter is set to False.