Home > Blockchain >  Removing a Key from a dict if value is not found in list
Removing a Key from a dict if value is not found in list

Time:10-14

I have a dictionary that has some keys that are not column names in my df this causes a KeyError to appear I would like to remove/ignore all the keys in the dictionary that are not matched to the column names

import pandas as pd
 
filename='template'
data= [['','','','','','','','Auto','','','','','']]
df= pd.DataFrame(data,columns=['first','last','state','lang','country','company','email','industry',
                                'System_Type__c','AccountType','segment','existing','permission'])
 
valid= {'industry': ['Automotive'],
        'SME Vertical': ['Agriculture'],
        'System_Type__c': ['Access'],
        'AccountType': ['Commercial']}
 
col_list=[col for col in df]
key = [k for k in valid if k in col_list]

I have saw some people use del or pop()

my deisred output would be something like

valid= {'industry': ['Automotive'],
        'System_Type__c': ['Access'],
        'AccountType': ['Commercial']}

How can I remove a key from a dictionary?

CodePudding user response:

here is one way to do it

# using dictionary comprehension, iterate through dict and 
# recreate dictionary when key exists in df.columns

valid={k:v for k, v in valid.items() if k in df.columns.values}
valid
{'industry': ['Automotive'],
 'System_Type__c': ['Access'],
 'AccountType': ['Commercial']}

CodePudding user response:

Because a dictionary is key value pairs deleting the key orphans the value effectively giving you what you want. Usage is like this:

del your_dict['your_key']

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

CodePudding user response:

Make sure to convert the lookup list to a set to get an O(n) operation instead of O(n^2):

valid = {'industry': ['Automotive'],
         'SME Vertical': ['Agriculture'],
         'System_Type__c': ['Access'],
         'AccountType': ['Commercial']}

to_keep = set(df.columns)

valid = {k: v for k, v in valid.items()
         if k in to_keep}
  • Related