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

Active January 26, 2022    /    Viewed 403    /    Comments 0    /    Edit


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

References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount