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

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 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

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

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