How to find all dataframe columns that contain a given word with pandas ?

Published: January 26, 2022

Updated: December 09, 2022

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

Examples of how to find all dataframe columns that contain a given word with pandas

Create a dataframe with pandas

Let's first create a dataframe with pandas

import pandas as pd
import numpy as np

data = np.arange(1,33)
data = data.reshape(4,8)

df = pd.DataFrame(data=data,columns=['name','address_01','address_02','address_03',
                                                                            'full address name','street address','zip code','county'])

print(df)

returns

     name  address_01  address_02  address_03  full address name  \
0     1           2           3           4                  5   
1     9          10          11          12                 13   
2    17          18          19          20                 21   
3    25          26          27          28                 29

     street address  zip code  county  
0               6         7       8  
1              14        15      16  
2              22        23      24  
3              30        31      32

Columns that contain the word "address"

df.filter(regex='address',axis=1) )

returns

     address_01  address_02  address_03  full address name  street address
0           2           3           4                  5               6
1          10          11          12                 13              14
2          18          19          20                 21              22
3          26          27          28                 29              30

Columns that end with the word "address"

    df.filter(regex='address$',axis=1)

returns

     street address
0               6
1              14
2              22
3              30

Columns that start with the word "address"

df.filter(regex='^address',axis=1)

returns

     address_01  address_02  address_03
0           2           3           4
1          10          11          12
2          18          19          20
3          26          27          28

Using a list comprehension

Another solution:

col_list = [col for col in df.columns if 'address' in col]

print( df[col_list ] )

returns

     address_01  address_02  address_03  full address name  street address
0           2           3           4                  5               6
1          10          11          12                 13              14
2          18          19          20                 21              22
3          26          27          28                 29              30