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 pdimport numpy as npdata = 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 51 9 10 11 12 132 17 18 19 20 213 25 26 27 28 29street address zip code county0 6 7 81 14 15 162 22 23 243 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 address0 2 3 4 5 61 10 11 12 13 142 18 19 20 21 223 26 27 28 29 30
Columns that end with the word "address"
df.filter(regex='address$',axis=1)
returns
street address0 61 142 223 30
Columns that start with the word "address"
df.filter(regex='^address',axis=1)
returns
address_01 address_02 address_030 2 3 41 10 11 122 18 19 203 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 address0 2 3 4 5 61 10 11 12 13 142 18 19 20 21 223 26 27 28 29 30
