Converting a pandas data-frame to a dictionary

0 votes

Let's say i have this 'Employee' data-set:

   Designation   Name
0   Developer    Raj
1         CTO    Sam
2         CEO    Suzy
3         CFO    Anne
4         COO    Rohan

I would want to convert this data-frame to a dictionary like this:

[{'Designation': 'Developer', 'Name': 'Raj'}, {'Designation': 'CTO', 'Name': 'Sam'}, {'Designation': 'CEO', 'Name': 'Suzy'}, {'Designation': 'CFO', 'Name': 'Anne'}, {'Designation': 'COO', 'Name': 'Rohan'}]

May 23, 2018 in Data Analytics by zombie
• 3,790 points
4,323 views

1 answer to this question.

0 votes
Emp_dict=Employee.to_dict('records')

You can directly use the 'to_dict()' function and use the parameter 'records', so that manual transposing is not required.

print(Emp.dict)
[{'Designation': 'Developer', 'Name': 'Raj'}, {'Designation': 'CTO', 'Name': 'Sam'}, {'Designation': 'CEO', 'Name': 'Suzy'}, {'Designation': 'CFO', 'Name': 'Anne'}, {'Designation': 'COO', 'Name': 'Rohan'}]
answered May 23, 2018 by Bharani
• 4,660 points

Related Questions In Data Analytics

0 votes
1 answer
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,242 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,442 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,418 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
12,972 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 1,638 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,485 views
0 votes
7 answers
0 votes
1 answer

Converting R data-frame to h2o object

All you have to do is run ...READ MORE

answered Apr 3, 2018 in Data Analytics by Bharani
• 4,660 points
2,344 views
0 votes
1 answer

Applying the same function to every row of a data.frame - R

You can use the 'appply()' function for ...READ MORE

answered May 22, 2018 in Data Analytics by Bharani
• 4,660 points
1,433 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP