Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Extract matching strings from a list

My dataframe has many columns. I want to extract columns starting with 9.
Code:

df_columns = Index(['_id', 'Time', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '2.8',
       '2.9', '2.10', '2.11', '2.12', '2.13', '2.14', '2.15', '2.16', '2.17',
       '2.18', '2.19', '2.20', '9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7',
       '9.8', '9.9', '9.10', '9.11', '9.12', '9.13', '9.14', '9.15', '9.16',
       '9.17', '9.18', '9.19', '9.20'],
      dtype='object')
col9s = df_columns.str.findall(r'\b9.')

Present solution:

cols = 
Index([    [],     [],     [],     [],     [],     [],     [],     [],     [],
           [],     [],     [],     [],     [],     [],     [],     [],     [],
           [],     [],     [],     [], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
       ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.'],
       ['9.'], ['9.'], ['9.'], ['9.'], ['9.'], ['9.']],
      dtype='object')

Expected answer:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

col9s = ['9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7',
       '9.8', '9.9', '9.10', '9.11', '9.12', '9.13', '9.14', '9.15', '9.16',
       '9.17', '9.18', '9.19', '9.20']

>Solution :

Use filter with a regex:

col9s = df.filter(regex=r'^9\.').columns

Or, to directly subset the columns:

df2 = df.filter(regex=r'^9\.')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading