Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.endswith() Method



TheSeries.str.endswith() method in Pandas is used to test if the end of each string element in a Series or Index matches a specified pattern. This method is useful for filtering and analyzing text data based on suffix patterns.

This method is similar to thestr.endswith() method in Python, providing an easy way to handle pattern matching at the end of strings within a Pandas Series or Index.

Syntax

Following is the syntax of the PandasSeries.str.endswith() method −

Series.str.endswith(pat, na=None)

Parameters

TheSeries.str.endswith() method accepts the following parameters −

  • pat − A string or tuple of strings representing the character sequence(s) to test for at the end of each element.

  • na − An optional object to show if the element tested is not a string. The default depends on the dtype of the array. For object-dtype,numpy.nan is used. ForStringDtype,pandas.NA is used.

Return Value

TheSeries.str.endswith() method returns a Series or Index of booleans indicating whether the given pattern matches the end of each string element.

Example 1

In this example, we demonstrate the basic usage of theSeries.str.endswith() method by testing if the strings in a Series end with the character 't'.

import pandas as pdimport numpy as np# Create a Series of stringss = pd.Series(['Python', 'Tutorialspoint', 'caT', np.nan])# Test if strings end with 't'result = s.str.endswith('t')print("Input Series:")print(s)print("\nSeries after calling str.endswith('t'):")print(result)

When we run the above code, it produces the following output −

Input Series:0           Python1    Tutorialspoint2              caT3              NaNdtype: objectSeries after calling str.endswith('t'):0    False1     True2    False3      NaNdtype: object

Example 2

This example demonstrates how to use theSeries.str.endswith() method to test if strings in a Series end with either 'n' or 'T'.

import pandas as pdimport numpy as np# Create a Series of stringss = pd.Series(['Python', 'Tutorialspoint', 'caT', np.nan])# Test if strings end with 'n' or 'T'result = s.str.endswith(('n', 'T'))print("Input Series:")print(s)print("\nSeries after calling str.endswith(('n', 'T')):")print(result)

Following is the output of the above code −

Input Series:0           Python1    Tutorialspoint2              caT3              NaNdtype: objectSeries after calling str.endswith(('n', 'T')):0     True1    False2     True3      NaNdtype: object

Example 3

TheSeries.str.endswith() method can also be applied to a DataFrame to check whether each element in a specified column ends with a given string or characters. The method returns a boolean Series object.

import pandas as pd# Creating a DataFrame for employeesemployee_df = pd.DataFrame({    'Employee_ID': ['E101', 'E102', 'E103', 'E104', 'E105'],    'Name': ['John', 'Emily', 'Mark', 'Sarah', 'Jessica'],    'Department': ['Sales', 'HR', 'IT', 'Marketing', 'Finance'],    'Salary': [50000, 60000, 75000, 80000, 90000]})# Check if 'Name' column values end with 'h'result = employee_df['Name'].str.endswith('h')print("Printing Original Employee DataFrame:")print(employee_df)print("\nBoolean Series after calling str.endswith('h') on 'Name' column:")print(result)

Following is the output of the above code −

Printing Original Employee DataFrame:  Employee_ID     Name Department  Salary0        E101     John      Sales   500001        E102    Emily         HR   600002        E103     Mark         IT   750003        E104    Sarah  Marketing   800004        E105  Jessica    Finance   90000Boolean Series after calling str.endswith('h') on 'Name' column:0     True1    False2    False3     True4    FalseName: Name, dtype: bool
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp