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

how can I select all columns of a dataframe, which partially match strings in a list?

Select all columns in df whose name partially match any of the strings in mylist. MRE:

import pandas as pd

# sample dataframe
df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6], 'ber': [7, 8, 9]})

# sample list of strings
mylist = ['oo', 'ba']

# desired output
df_out = {'foo': [1, 2, 3], 'bar': [4, 5, 6]}

>Solution :

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

You can use df.filter with regex to do that.

import pandas as pd

# sample dataframe
df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6], 'ber': [7, 8, 9]})

# sample list of strings
mylist = ['oo', 'ba']

# join the list to a single string
matches = '|'.join(mylist)

# use regex to filter the columns based on the string
df_out = df.filter(regex=matches)
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