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 pandas: filter columns in dataframe BEGINNING with pattern

I have 2 columns called eta and theta in a pandas data frame.

I’m trying to select the column called eta only.

So far I have tried

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

df.filter(like='eta')

…however this selects both eta and theta.

I’ve also tried pattern, but this is an R command:

df.filter(pattern='^eta')

>Solution :

Use the regex parameter of filter:

df.filter(regex=r'^eta')

Make sure to escape regex characters if needed, for instance if the columns start with eta.:

df.filter(regex=r'^eta\.')

If you have a doubt:

import re

target = 'eta'
df.filter(regex=fr'^{re.escape(target)}')
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