Open In App
Related Articles

Check if a column ends with given string in Pandas DataFrame

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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', 'GeeksforGeeks'
                'GeeksforGeeks', '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



Last Updated : 07 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads