Wednesday, October 22, 2025
HomeLanguagesCheck if a column ends with given string in Pandas DataFrame

Check if a column ends with given string in Pandas DataFrame

In this program, we are trying to check whether the specified column in the given data frame ends with the specified string or not. Pandas endswith() is yet another method to search and filter text data in a DataFrame. This method is similar to Pandas.startwith() method.

Syntax: string.endswith(value) 

Creating Dataframe to Check If a Column Ends with a given String or Not

Let’s try to understand this using an example suppose we have an employee dataset named Company_id, Joining_Date, Company, companyEmp_id.

Python3




# importing library pandas as pd
import pandas as pd
  
# creating data frame for employee
df = pd.DataFrame({
    'Company_id': ['IF101', 'IF103', 'GFG671'
                   'GFG881', 'CAPG961', 'WPR981'],
    
    'Company': ['Infosys', 'Infosys', 'Lazyroar'
                'Lazyroar', 'Capgemini', 'Wipro'],
      
    'Joining_Date': ['12/12/2011', '07/12/2012'
                        '11/11/2014', '09/12/2015'
                        '01/01/2016', '01/01/2009'],
    
    'Company_Emp_id': ['abc@infosys.com', 'xyz@infosys.com'
                       'acd@gfg.com', 'aed@gfg.com'
                       'klm@capg.com', 'xyz@wipro.com']
})
# printing the given data frame
print("Printing Original Student DataFrame:")
  
df


Output: 

Check if a column ends with given string

 

Example 1:

The endswith() method in Python returns True if the string ends with the gfg.com, if not it returns False.

Python3




# joining new column in dataframe 
# endwith function used to check
df['GFG_Emp'] = list(
    map(lambda x: x.endswith('gfg.com'), df['Company_Emp_id'])) 
    
# printing new data frame
df


Output: 
 

 

Example 2:

The endswith() method in Python returns True if the string ends with infosys.com, if not it returns False.

Python3




# joining new column in dataframe 
# .startswith function used to check
df['GFG_Emp'] = list(
    map(lambda x: x.endswith('Infosys'), df['Company'])) 
    
# printing new data frame
df


Output:

Pandas Series.str.endswith()

 

RECOMMENDED ARTICLES – Check if a column starts with given string in Pandas DataFrame

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS