You can use the following methods to select columns that contain a particular string in a pandas DataFrame:
Method 1: Select Columns that Contain One Specific String
df.filter(regex='string1')
Method 2: Select Columns that Contain One of Several Strings
df.filter(regex='string1|string2|string3')
The following examples show how to use each of these methods in practice with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'mavs': [10, 12, 14, 15, 19, 22, 27],
'cavs': [18, 22, 19, 14, 14, 11, 20],
'hornets': [5, 7, 7, 9, 12, 9, 14],
'spurs': [10, 12, 14, 13, 13, 19, 22],
'nets': [10, 14, 25, 22, 25, 17, 12]})
#view DataFrame
print(df)
mavs cavs hornets spurs nets
0 10 18 5 10 10
1 12 22 7 12 14
2 14 19 7 14 25
3 15 14 9 13 22
4 19 14 12 13 25
5 22 11 9 19 17
6 27 20 14 22 12
Example 1: Select Columns that Contain One Specific String
The following code shows how to use the filter() function to select only the columns that contain the string “avs” somewhere in their name:
#select columns that contain 'avs' in the name
df2 = df.filter(regex='avs')
#view DataFrame
print(df2)
mavs cavs
0 10 18
1 12 22
2 14 19
3 15 14
4 19 14
5 22 11
6 27 20
Only the columns that contain “avs” in the name are returned.
In this case, “mavs” and “cavs” are the only columns that are returned.
Example 2: Select Columns that Contain One of Several Strings
The following code shows how to use the filter() function to select only the columns that contain “avs” or “ets” somewhere in their name:
#select columns that contain 'avs' in the name
df2 = df.filter(regex='avs|ets')
#view DataFrame
print(df2)
mavs cavs hornets nets
0 10 18 5 10
1 12 22 7 14
2 14 19 7 25
3 15 14 9 22
4 19 14 12 25
5 22 11 9 17
6 27 20 14 12
Only the columns that contain “avs” or “ets” in the name are returned.
Note that the vertical bar ( | ) is the “OR” operator in pandas.
Feel free to chain together as many of these “OR” operators as you’d like to select columns that contain one of several different strings.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: How to Move Column to Front of DataFrame
Pandas: How to Check if Column Contains String
Pandas: How to Add Empty Column to DataFrame (3 Examples)