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'}]
Code faster with devsheet chrome extension and increase productivity as a programmer
search code from your favorite search engine instantly.
Install Now

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
Open Code Manager We are hiring