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,770 points
3,294 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,580 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
427 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
485 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
• 32,490 points
500 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
7,276 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 by MD 525 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 by Gitika 1,257 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 2, 2018 in Data Analytics by Bharani
• 4,580 points
1,356 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,580 points
475 views