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,690 points
1,139 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,550 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
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,700 points
58 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,700 points
66 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
86 views
0 votes
2 answers
0 votes
1 answer

How to rename columns in pandas (Python)?

It is easy by just adding ".columns" ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,700 points
86 views
0 votes
1 answer

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

The major difference is size includes NaN ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,700 points
674 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,550 points
494 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,550 points
39 views