Change language

Python | Pandas Series.str.endswith ()

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.

# import pandas module

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# String to search at the end of a line

search = "e" 

 
# boolean series returns with False in place of NaN

bool_series = data [ "College" ]. str . lower () . str . endswith (search)

  
# logical series display
bool_series

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.

# pandas module import

import pandas as pd

 
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# String to search at the end of the line

search = "e"

 
# boolean series returns with False in place of NaN

bool_series = data [ " College " ]. str . Lower (). str . endswith (search, na = False )

 
# display the filtered data frame
data [bool_series]

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. 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically