Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Big Data Hadoop & Spark by (19k points)

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'}]

1 Answer

0 votes
by (33.1k points)

Refer to this code example:

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'}]

Hope this answer helps you!

Browse Categories

...