nos codemos
nos codemos

Reputation: 671

fill a dictionary from rows in a Pandas dataframe

I would like to populate a multitude of dictionaries with values contained in a dataframe. Ideally, each row should be used to fill one dictionary, which will be appended to a list of dictionaries.

#example df
firstname | lastname | dob        | modified   | created
john        smith      1990-01-01   2019-10-11   2015-06-25
becca       meyers     1995-04-13   2020-01-08   2018-05-18

each dictionary should look like this: (please ignore the fact this makes no sense given the example, it's just how it has to be done)

d = {
  "firstname" : {
      "value": df['firstname'],
      "modified": df['modified'],
      "created": df['created']
   },{
   "lastname": {
     "value": df['lastname'],
     "modified": df['modified'],
     "created": df['created']
   }
}

I have tried doing a for loop through the df, but it just populated the whole dataframe in each field, which is not what I was hoping for.

l = []
for x in df:
    d = {
        "firstname" : {
            "value": df['firstname'],
            "modified": df['modified'],
            "created": df['created']
        },{
        "lastname": {
            "value": df['lastname'],
            "modified": df['modified'],
            "created": df['created']
           }
       }
    l.append(d)

ninjaedit: my anticipated output should be:

l = [{ 
   "firstname": {
   "value": "john",
   "modified": 2019-10-11,
   "created": 2015-06-25}
   },{
   "lastname": {
   "value": "smith",
   "modified": 2019-10-11,
   "created": 2015-06-25}},
   ...
}]

It's been a couple of months since I've done anything python/pandas so I'm a bit out of practice. Thanks in advance. Sorry if this is confusing, I can expand if need be.

Upvotes: 1

Views: 719

Answers (2)

jezrael
jezrael

Reputation: 863226

Use DataFrame.itertuples:

l = []
for x in df.itertuples():
    d = {
        "firstname" : {
            "value": x.firstname,
            "modified": x.modified,
            "created": x.created
        },
        "lastname": {
            "value": x.lastname,
            "modified": x.modified,
            "created": x.created
            }
        }
    l.append(d)
print (l)

[{'firstname': {'value': 'john', 'modified': '2019-10-11', 'created': '2015-06-25'}, 
  'lastname': {'value': 'smith', 'modified': '2019-10-11', 'created': '2015-06-25'}}, 
 {'firstname': {'value': 'becca', 'modified': '2020-01-08', 'created': '2018-05-18'}, 
  'lastname': {'value': 'meyers', 'modified': '2020-01-08', 'created': '2018-05-18'}}]

Slowier alternative:

l = []
for i, x in df.iterrows():
    d = {
        "firstname" : {
            "value": x.firstname,
            "modified": x.modified,
            "created": x.created
        },
        "lastname": {
            "value": x.lastname,
            "modified": x.modified,
            "created": x.created
            }
        }
    l.append(d)

Upvotes: 3

Zviad
Zviad

Reputation: 69

you should use df.iterrows() in loop

Here little article about this how iterate pandas dataframe

Upvotes: 0

Related Questions