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

pandas filtering column names

I have this type of dataframe;

A = ["axa","axb","axc","axd","bxa","bxb","bxc","bxd","cxa".......]

My question is I have this type of data but there are more than 350 columns and for example i need only ‘c’ including column names in new dataframe. How can i do that?

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

new dataframe columns should look like this;

B = A[["axc","bxc","cxa","cxb","cxc","cxd","dxc","exc","fxc".......]]

>Solution :

Use for filter columns names with c by DataFrame.filter:

df2 = df.filter(like='c')

Or use list comprehension for filter columns names:

df2 = df[[x for x in df.columns if 'c' in x]]
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