Search code examples
pythondictionarylist-comprehensiondict-comprehension

How do you build a nested dict comprehension that imitates 'records' made by '.to_dict'?


I have read a CSV file into Pandas and converted the resulting dataframe into a list of dictionaries for each row using the 'to_dict(orient='records') function. A shortened version of the list looks like this:

records = [{'addjob': 'ADDJOB',
  'age': 'AGE',
  'disab': 'DISCURR13',
  'eth': 'ETHUKEUL',
  'full': 'FTPT'},
{'addjob': 'ADDJOB',
  'age': 'AGE',
  'disab': 'DISCURR13',
  'eth': 'ETHUKEUL',
  'full': nan}]

I am trying to imitate this structure by using a dict comprehension like so:

    cleaned_records = OrderedDict([
        {k:v for k,v in i} for i in records
    ])

EDIT: removed 'OrderedDict' as it was a mistake (error is the same):

    cleaned_records = [{k:v for k,v in i} for i in records]

However, it is giving me the following error:

enter image description here

The reason I am trying to do this is so I can remove those keys from the dictionaries whose values are null before passing them to another set of functions.

I've been at this for quite a while now and I'm baffled as to why this dict comprehension is not working. Can anyone help me?


Solution

  • You're just missing the .items() or .iteritems() on the dict extraction.

    In [28]: [{k:v for k,v in i.iteritems()} for i in records]
    Out[28]: 
    [{'addjob': 'ADDJOB',
      'age': 'AGE',
      'disab': 'DISCURR13',
      'eth': 'ETHUKEUL',
      'full': 'FTPT'},
     {'addjob': 'ADDJOB',
      'age': 'AGE',
      'disab': 'DISCURR13',
      'eth': 'ETHUKEUL',
      'full': nan}]