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

Select dataframe columns that starts with certain string and additional columns

I have a dataframe with columns: 'Id', 'Category', 'Shop', ....., 'Brandtxsu1', 'Brandxyw2', ...

I want to select columns: ID, Category, and start with Brand. I can select the columns that start with Brand using the following code, but how do I select ID and Category?

df[df.columns[pd.Series(df.columns).str.startswith('Brand')]]

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

>Solution :

You can try join with filter

out = df[['ID', 'Category']].join(df.filter(regex='^Brand'))
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