Home > database >  How to check if the first column names of my data frame exist in a particular oder
How to check if the first column names of my data frame exist in a particular oder

Time:02-21

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?

CodePudding user response:

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!='????')
  • Related