Reputation: 431
I have the following data, but I want the final output to remove the keys and just be a list of dictionaries.
df = pd.DataFrame({'id':[1,2,3,4,5],'firstname':['John','Gerry','Sarah','Pierre','Angela'],
'lastname': ['Doe','Ronald','Lee','Sam','Perez']})
df = df.T
df = [df.to_dict()]
[{0: {'id': 1, 'firstname': 'John', 'lastname': 'Doe'},
1: {'id': 2, 'firstname': 'Gerry', 'lastname': 'Ronald'},
2: {'id': 3, 'firstname': 'Sarah', 'lastname': 'Lee'},
3: {'id': 4, 'firstname': 'Pierre', 'lastname': 'Sam'},
4: {'id': 5, 'firstname': 'Angela', 'lastname': 'Perez'}}]
Desired output:
[{'id': 1, 'firstname': 'John', 'lastname': 'Doe'},
{'id': 2, 'firstname': 'Gerry', 'lastname': 'Ronald'},
{'id': 3, 'firstname': 'Sarah', 'lastname': 'Lee'},
{'id': 4, 'firstname': 'Pierre', 'lastname': 'Sam'},
{'id': 5, 'firstname': 'Angela', 'lastname': 'Perez'}]
Upvotes: 2
Views: 42
Reputation: 14949
You can try orient=records
:
result = df.to_dict(orient='records')
OUTPUT:
[{'id': 1, 'firstname': 'John', 'lastname': 'Doe'},
{'id': 2, 'firstname': 'Gerry', 'lastname': 'Ronald'},
{'id': 3, 'firstname': 'Sarah', 'lastname': 'Lee'},
{'id': 4, 'firstname': 'Pierre', 'lastname': 'Sam'},
{'id': 5, 'firstname': 'Angela', 'lastname': 'Perez'}]
Upvotes: 2