Examples of how to select dataframe columns that start with *** using pandas in python:
Create a dataframe
Lets start by creating a simple dataframe with 8 columns:
import pandas as pdimport numpy as npdata = np.arange(1,33)data = data.reshape(4,8)df = pd.DataFrame(data=data,columns=['name','add_01','add_02','add_03','counrty','streed','zip code','county'])print(df)
returns
name add_01 add_02 add_03 counrty streed zip code county0 1 2 3 4 5 6 7 81 9 10 11 12 13 14 15 162 17 18 19 20 21 22 23 243 25 26 27 28 29 30 31 32
Select the column that start by "add" (option 1)
To select here the column that start by the work "add" in the above datframe, one solution is to create a list of columns name that start with 'add' (Note: to get a list of dataframe column name just use df.columns):
list = [col for col in df.columns if col.startswith('add')]
and then:
df[list]
which returns
add_01 add_02 add_030 2 3 41 10 11 122 18 19 203 26 27 28
Select the column that start by "add" (option 2)
Another option using a pandas serie:
df[df.columns[pd.Series(df.columns).str.startswith('add')]]
returns
add_01 add_02 add_030 2 3 41 10 11 122 18 19 203 26 27 28
Using filter (option 3)
Another solution is to use filter:
df.filter(regex='^add',axis=1)
returns as well
add_01 add_02 add_030 2 3 41 10 11 122 18 19 203 26 27 28
