0

How do I ignore missing keys in a pd.DataFrame.from_dict comprehension iteration?

@jezrael kindly answered my problem on making a df with dictionary values here:

https://stackoverflow.com/a/49072184/9242601

But I encountered a Key Error: 'fees' error because not all "Customers" have a 'fees' key, so rather than generating an error, I'd like the iteration to just move on to the next Customer and not record that customer in the df.

I still need a dataframe, so try... except and if key in dict won't work, because I will just end up with an empty (or non-existent) df.

Apologies for effectively spreading this question over two questions, but I didn't think it was worth repeating the complete initial question (hence the link).

Thanks.

ACF
  • 51
  • 1
  • 11
  • 2
    `if key in dct` – user3483203 Mar 02 '18 at 20:52
  • 1
    or `{k: dict(v.get('fees')) for k, v in d.items()}` if you don't mind storing `None` as the value for items lacking the key. – Paulo Scardine Mar 02 '18 at 20:53
  • or `df = pd.DataFrame.from_dict({k: {}.setdefault('fees', v.get('fees',[])) for k, v in d.items()}, orient='index')` - providing an empty default fees list if none is present - not sure if that fits into your data – Patrick Artner Mar 02 '18 at 20:58
  • 2
    Possible duplicate of [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – user3483203 Mar 02 '18 at 21:00

2 Answers2

2

The link's OK - it's in Stackoverflow and isn't going anywhere.

Meet the dict.get method. By way of a pre-amble, this is almost everything you need to know about it:

>>> d = {'present': 'real_value'}
>>> d.get('present')
'real_value'
>>> d.get('absent') is None
True
>>> d.get('absent', 'default')
'default'

You should find that the modified comprehension

{k: dict(v['fees']) for k, v in d.items() if v.get('fees') is not None}

serves your needs, but this is untested code. Possibly more readable is

{k: dict(v['fees']) for k, v in d.items() if 'fees' in v}
holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

There's a couple different ways to solve this.

One way is to use a try except statement to catch the error.

Another way is to test is the key is in the dict key in dict.

Another would be to use the dict's get method dict.get(key, default = None) where the default option will return None if the value is not found or you can set it to want you need.

Mike Peder
  • 728
  • 4
  • 8