Skip to content
Advertisement

Checking if column headers match PYTHON

I have two dataframes:

df1:

JavaScript

df2

JavaScript

I want to write a function that checks if the column headers are matching/the same as columns in df1.

IF not we get a message telling us what column is missing.

Example of the message given these dataframes:

JavaScript

I want a generalized code that can work for any given dataframe.

Is this possible on python?

Advertisement

Answer

You can have access to the column names via .columns and then use set operations to check what you want:

JavaScript

And it gives the expected output:

JavaScript
Advertisement