How to select dataframe columns that start with *** using pandas in python ?

How to select dataframe columns that start with *** using pandas in python ?

Daidalos May 24, 2020


Examples of how to select dataframe columns that start with *** using pandas in python:

1 -- Create a dataframe

Lets start by creating a simple dataframe with 8 columns:

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','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  county
0     1       2       3       4        5       6         7       8
1     9      10      11      12       13      14        15      16
2    17      18      19      20       21      22        23      24
3    25      26      27      28       29      30        31      32

2 -- 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_03
0       2       3       4
1      10      11      12
2      18      19      20
3      26      27      28

3 -- 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_03
0       2       3       4
1      10      11      12
2      18      19      20
3      26      27      28

4 -- References