python - Find column whose name contains a specific string

ID : 20354

viewed : 16

Tags : pythonpython-3.xstringpandasdataframepython

Top 5 Answer for python - Find column whose name contains a specific string

vote vote

97

Just iterate over DataFrame.columns, now this is an example in which you will end up with a list of column names that match:

import pandas as pd  data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]} df = pd.DataFrame(data)  spike_cols = [col for col in df.columns if 'spike' in col] print(list(df.columns)) print(spike_cols) 

Output:

['hey spke', 'no', 'spike-2', 'spiked-in'] ['spike-2', 'spiked-in'] 

Explanation:

  1. df.columns returns a list of column names
  2. [col for col in df.columns if 'spike' in col] iterates over the list df.columns with the variable col and adds it to the resulting list if col contains 'spike'. This syntax is list comprehension.

If you only want the resulting data set with the columns that match you can do this:

df2 = df.filter(regex='spike') print(df2) 

Output:

   spike-2  spiked-in 0        1          7 1        2          8 2        3          9 
vote vote

80

This answer uses the DataFrame.filter method to do this without list comprehension:

import pandas as pd  data = {'spike-2': [1,2,3], 'hey spke': [4,5,6]} df = pd.DataFrame(data)  print(df.filter(like='spike').columns) 

Will output just 'spike-2'. You can also use regex, as some people suggested in comments above:

print(df.filter(regex='spike|spke').columns) 

Will output both columns: ['spike-2', 'hey spke']

vote vote

79

You can also use df.columns[df.columns.str.contains(pat = 'spike')]

data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]} df = pd.DataFrame(data)  colNames = df.columns[df.columns.str.contains(pat = 'spike')]   print(colNames) 

This will output the column names: 'spike-2', 'spiked-in'

More about pandas.Series.str.contains.

vote vote

69

# select columns containing 'spike' df.filter(like='spike', axis=1) 

You can also select by name, regular expression. Refer to: pandas.DataFrame.filter

vote vote

56

df.loc[:,df.columns.str.contains("spike")] 

Top 3 video Explaining python - Find column whose name contains a specific string

Related QUESTION?