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 to check if the first column names of my data frame exist in a particular oder

An application I am developing generates at some point data frames in which most of the names of the columns should be exactly the same apart from one column. I am doing some validation and I would like to check if all columns names are in the position they should be.

The problem is that there is one column whose name will change and I cannot test that.

header_list = ['one','two','three','????','four','five']

what is the simplest way to do this?

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 :

If you know the position of the variable column, use:

l = list(df.columns)
l.pop(3)
assert l == ['one','two','three','four','five']

more generic approach to enable many "blanks":

header_list = ['one','two','three','????','four','five']
assert all(col==ref for col,ref in zip(df, header_list) if ref!='????')
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