0
$\begingroup$

I have Dataframe as below.

name    value     id
A       123         1
B       345         5
C       567         4
D       789         2

I need to create a dictionary of the form below

{
  {"name":"A",
   "value":123,
   "id":1
  },
  {"name":"B",
   "value":345,
   "id":5
  },
  {"name":"C",
   "value":567,
   "id":4
  },
  {"name":"D",
   "value":789,
   "id":2
  }
}

I tried below code, but it makes 'name' as the key.

data_set.set_index('name').to_dict(orient='list')

How to get the dictionary of the form i have mention? Do i need to iterate through rows?

$\endgroup$
2
$\begingroup$

Check this:

data_set.to_dict(orient = 'records')
$\endgroup$
  • 1
    $\begingroup$ Thank you. this is exactly what i needed $\endgroup$ – Harshith Aug 19 '19 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.