w3resource

Pandas Series: str.startswith() function

Series-str.startswith() function

The str.startswith() function is used to test if the start of each string element matches a pattern.
The function is equivalent to str.startswith().

Syntax:

Series.str.startswith(self, pat, na=nan)
Pandas Series: str.startswith() 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 NaN Required

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

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['dog', 'Deer', 'fox', np.nan])
s

Output:

0     dog
1    Deer
2     fox
3     NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['dog', 'Deer', 'fox', np.nan])
s.str.startswith('d')

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(['dog', 'Deer', 'fox', np.nan])
s.str.startswith('d', na=False)

Output:

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

Previous: Series-str.rsplit() function
Next: Series-str.strip() 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://www.w3resource.com/pandas/series/series-str-startswith.php