Home > Mobile >  How can I ignore missing keys in a dict comprehension?
How can I ignore missing keys in a dict comprehension?

Time:02-17

I'm trying to count the number of instances of a particular value in a dictionary using the index from a DataFrame as the key. Some of the DataFrame indexes however are missing from the set of dictionary keys. How can I construct a dictionary comprehension to overcome this?

import pandas as pd

df = pd.DataFrame({'Date': ['2022-02-14', '2022-02-14', '2022-02-14', '2022-02-14', '2022-02-14'],
'count': [10, 10, 10, 9, 9],},
index = ['NNI', 'NVEC', 'IPA', 'LYTS', 'MYN'])

df
            Date  count
NNI   2022-02-14     10
NVEC  2022-02-14     10
IPA   2022-02-14     10
LYTS  2022-02-14      9
MYN   2022-02-14      9

dct = {'NNI' : pd.DataFrame({'s': [-1, -1, -1],
'count': [13, 11, 10]},
index =['2007-07-13', '2019-09-18', '2016-08-01']),
'NVEC' : pd.DataFrame({'s': [-1, -1, -1],
'count': [12, 10, 9]},
index =['2012-10-09', '2018-10-01', '2022-02-01'])
}

dct
{'NNI':        s  count
 2007-07-13   -1     13
 2019-09-18   -1     11
 2016-08-01   -1     10,
 'NVEC':       s  count
 2012-10-09   -1     12
 2018-10-01   -1     10
 2022-02-01   -1      9}

I tried the below without luck.

{k:len(dct[k]['count']) for k in df.index if k in df.index}

KeyError                                  Traceback (most recent call last)
<ipython-input-201-10244dbbde59> in <module>
----> 1 {k:len(dct[k]['count']) for k in df.index if k in df.index}

<ipython-input-201-10244dbbde59> in <dictcomp>(.0)
----> 1 {k:len(dct[k]['count']) for k in df.index if k in df.index}

KeyError: 'IPA'

CodePudding user response:

Your if clause should involve dct instead of df.index if i understood you correctly

{k:len(dct[k]['count']) for k in df.index if k in dct.keys()}

Should work.

If your dict is pretty large - you may want to get all keys first and then iterate over them, like:

dct_keys = dct.keys()
{k:len(dct[k]['count']) for k in df.index if k in dct_keys}
  • Related