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

Comparing Dictionary Key to df column names to find missing columns

I want to compare the keys in the dictionary to the df column names to find the missing column names.

import pandas as pd

df= pd.DataFrame(columns=['industry','System_Type__c','AccountType','email','firstname','lastName','country','company'])
req_cols={"firstname":[],"lastName":[],"country":[],"company":[],"email":[], "Existing_Customer__c":[]}

errors= {}

if req_cols.items() in df.columns.values:
    pass
else:
    errors= {"There is a missing required column"}

I have the above code to write an error message but I would like it to be more detailed. Here’s an example of the error message I would like

{'Existing_Customer_c is missing in the column names'}

obviously the output would change depending on what column value is missing and if not value is missing than no error message needs to be wrote.

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

How would I compare the dictionary key to the df column names to find which variable is missing and write that error message?

>Solution :

You can use set operations:

missing = set(req_cols)-set(df)

if missing:
    errors = {f'missing columns: {", ".join(missing)}'}
else:
    errors = {}
    
print(errors)

Output: {'missing columns: Existing_Customer__c'}

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