In this tutorial, we will look at how to get the column names in a pandas dataframe that end with a specific string (in the column name) with the help of some examples.
How to find columns whose name ends with a specific string?

You can apply the string endswith()
function with the help of the .str
accessor on df.columns
to check if column names (of a pandas dataframe) end with a specific string.
You can use the .str
accessor to apply string functions to all the column names in a pandas dataframe.
Pass the end string as an argument to the endswith()
function. The following is the syntax.
# get column names that end with a specific string, s df.columns[df.columns.str.endswith(s)]
The idea is to get a boolean array using df.columns.str.endswith()
and then use it to filter the column names in df.columns
.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Alternatively, you can use a list comprehension to iterate through the column names and check if the column name ends with the specified string or not.
Examples
Let’s now look at some examples of using the above syntax.
First, we will create a pandas dataframe that we will be using throughout this tutorial.
import pandas as pd # employee data data = { "Name_Emp": ["Jim", "Dwight", "Angela", "Tobi"], "Age_Emp": [26, 28, 27, 32], "Department": ["Sales", "Sales", "Accounting", "HR"] } # create pandas dataframe df = pd.DataFrame(data) # display the dataframe df
Output:

Here, we created a dataframe with information about some employees in an office. The dataframe has the columns – “Name_Emp”, “Age_Emp”, and “Department”.
Example 1 – Get column names that end with a specific string
Let’s get the column names in the above dataframe that end with the string “_Emp” in their column labels.
We’ll apply the string endswith()
function with the help of the .str
accessor to df.columns
.
# check if column name ends with the string, "_Emp" df.columns.str.endswith("_Emp")
Output:
array([ True, True, False])
You can see that we get a boolean array indicating which columns in the dataframe end with the string “_Emp”.
We can use the above boolean array to filter df.columns
to get only the columns that end with the specified string (in this example, “_Emp”)
# get column names that end with the string, "_Emp" df.columns[df.columns.str.endswith("_Emp")]
Output:
Index(['Name_Emp', 'Age_Emp'], dtype='object')
We get the column names ending with “_Emp” in the above dataframe.
Example 2 – Get column names that end with a specific string using list comprehension
Alternatively, we can use a list comprehension to iterate through the column names in df.columns
and select the ones that end with the given string.
# get column names that end with the string, "_Emp" [col for col in df.columns if col.endswith("_Emp")]
Output:
['Name_Emp', 'Age_Emp']
We get the column names that end with “_Emp”. The “Name_Emp” and the “Age_Emp” columns are the only ones that end with the string “_Emp” in the above dataframe.
Summary
In this tutorial, we looked at how to get the column names that end with a specified string in a pandas dataframe. The following are the key takeaways –
- Use the string
endswith()
function (applied using the.str
accessor ondf.columns
) to check if a column name ends with a given string or not (and use this result to filterdf.columns
). - You can also get column names that end with a specified string with the help of a list comprehension.
You might also be interested in –
- Pandas – Find Column Names that Contain Specific String
- Pandas – Get Columns with Missing Values
- Pandas – Add Prefix to Column Names
- Pandas – Add Suffix to Column Names
- Pandas – Change Column Names to Uppercase
- Pandas – Change Column Names to Lowercase
- Remove Prefix or Suffix from Pandas Column Names
- Get Column Names as List in Pandas DataFrame
- Pandas – Rename Column Names
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.