Manolo Dominguez Becerra
Manolo Dominguez Becerra

Reputation: 1373

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?

Upvotes: 0

Views: 308

Answers (1)

mozway
mozway

Reputation: 261974

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

Upvotes: 1

Related Questions