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.
How would I compare the dictionary key to the df column names to find which variable is missing and write that error message?
CodePudding user response:
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'}