w3resource

Pandas Series: str.endswith() function

Series-str.endswith() function

The str.endswith() function is used to test if the end of each string element matches a pattern.

Equivalent to str.endswith()

Syntax:

Series.str.endswith(self, pat, na=nan)
Pandas Series: str.endswith() function

Parameters:

Name Description Type/Default Value Required / Optional
pat  Character sequence. Regular expressions are not accepted. str Required
na Object shown if element tested is not a string. object
Default Value: NaN
Required

Returns: Series or Index of bool
A Series of booleans indicating whether the given pattern matches the end of each string element.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['bag', 'mouse', 'biG', np.nan])
s

Output:

0      bag
1    mouse
2      biG
3      NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['bag', 'mouse', 'biG', np.nan])
s.str.endswith('g')

Output:

0     True
1    False
2    False
3      NaN
dtype: object

Example - Specifying na to be False instead of NaN:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['bag', 'mouse', 'biG', np.nan])
s.str.endswith('g', na=False)

Output:

0     True
1    False
2    False
3    False
dtype: bool
Pandas Series: str.endswith() function

Previous: Series-str.count() function
Next: Series-str.extract() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-str-endswith.php