Data Analysis
Create a
dataframe
from dictionary in Pandas
:import pandas
data = [{'id': 1, 'name': 'alireza'}, {'id': 2, 'name': 'Mohsen'}]
# Creating a dataframe from a dictionary object
df = pandas.DataFrame(data)
Now if you print dataframe:
> df
id name
0 1 alireza
1 2 Mohsen
NOTE:
the first column is the index column.In order to turn it to a dictionary after your aggregation, analysis, etc just use
to_dict
like below:df.to_dict(orient='records')
[{'id': 1, 'name': 'alireza'}, {'id': 2, 'name': 'Mohsen'}]
You are right! We didn't do anything useful on records, but the goal is to tell you how to turn dataframe to a dictionary not more.
NOTE:
on older version of pandas you have to use outtype='records'
rather than orient='records'
.#python #pandas #to_dict #outtype #orient #dictionary #dataframe