Python | Pandas Series.str.endswith() - GeeksforGeeks

17 Sep, 2018
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 acceptedna: 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 seriesIn 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 data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")  # String to be searched in end of string search ="e"  # boolean series returned with False at place of NaNbool_series = data["College"].str.lower().str.endswith(search)  # displaying boolean seriesbool_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 data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")  # String to be searched in end of string search ="e"  # boolean series returned with False at place of NaNbool_series = data["College"].str.lower().str.endswith(search, na = False)  # displaying filtered dataframedata[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.
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()