Open In App

Python | Pandas Series.str.endswith()

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas endswith() is yet another method to search and filter text data in a Series or a Data Frame. This method is Similar to Python’s endswith() method, but has different parameters and it works on Pandas objects only. Hence .str has to be prefixed every time before calling this method, so that the compiler knows that it’s different from default function.

Syntax: Series.str.endswith(pat, na=nan)

Parameters:
pat: String to be searched. Regex are not accepted
na: Used to set what should be displayed if the value in series is NULL.

Return type: Boolean series which is True where the value has the passed string in the end.

To download the CSV used in code, click here.

In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.

Example #1: Returning Bool series
In this example, the college column is checked if elements have “e” in the end of string using the str.endswith() function. A Boolean series is returned which is true at the index position where string has “e” in the end. str.lower() method is called before endswith() since the data can be in any case.




# importing pandas module 
import pandas as pd
  
# reading csv file from url 
  
# String to be searched in end of string 
search ="e"
  
# boolean series returned with False at place of NaN
bool_series = data["College"].str.lower().str.endswith(search)
  
# displaying boolean series
bool_series


Output:
As shown in the output image, The bool series is having True at the index position where the College column was having “e” in the end. It can also be compared by looking at the image of original data frame.

 
Example #2: Handling NULL values

The most important part in data analysis is handling Null values. As it can be seen in the above output image, the Boolean series is having NaN wherever the value in College column was empty or NaN. If this boolean series is passed into data frame, it will give an error. Hence, the NaN values need to be handled using na Parameter. It can be set to string too, but since bool series is used to pass and return respective value, it should be set to a Bool value only.
In this example, na Parameter is set to False. So wherever the College column is having Null value, the Bool series will store False instead of NaN. After that, the series is passed again to data frame to display only True values.




# importing pandas module 
import pandas as pd
  
# reading csv file from url 
  
# String to be searched in end of string 
search ="e"
  
# boolean series returned with False at place of NaN
bool_series = data["College"].str.lower().str.endswith(search, na = False)
  
# displaying filtered dataframe
data[bool_series]


Output:
As shown in the output image, the data frame is having rows which have “e” in the end of the string in College column. NaN values are not displayed since the na parameter was set to False.



Last Updated : 17 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads