Chrome Extension

Convert pandas DataFrame to List of dictionaries python

You can pass 'records' as a parameter to to_dict() method of the pandas data frame to convert a dataframe to the list of dictionaries.
import pandas as pd

df_fruits = pd.DataFrame()
df_fruits["fruit_name"] = ["Orange", "Banana", "Apple"]
df_fruits["color"] = ["orange", "yellow", "red"]

print(df_fruits.to_dict('records'))
# prints - [{'fruit_name': 'Orange', 'color': 'orange'}, {'fruit_name': 'Banana', 'color': 'yellow'}, {'fruit_name': 'Apple', 'color': 'red'}]
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Search Index Data (The code snippet can also be found with below search text)

dataframe to array of objects pandas
Was this helpful?
0 Comments