chevron_left
Miscellaneous Cookbook
Adjusting number of rows that are printedAppending DataFrame to an existing CSV fileChecking differences between two indexesChecking if a DataFrame is emptyChecking if a variable is a DataFrameChecking if index is sortedChecking if value exists in IndexChecking memory usage of DataFrameChecking whether a Pandas object is a view or a copyConcatenating a list of DataFramesConverting a DataFrame to a listConverting a DataFrame to a SeriesConverting DataFrame to a list of dictionariesConverting DataFrame to list of tuplesCounting the number of negative valuesCreating a DataFrame using cartesian product of two DataFramesDisplaying DataFrames side by sideDisplaying full non-truncated DataFrame valuesDrawing frequency histogram of DataFrame columnHighlighting a particular cell of a DataFrameHighlighting DataFrame cell based on valueHow to solve "ValueError: If using all scalar values, you must pass an index"Plotting two columns of DataFramePrinting DataFrame on a single linePrinting DataFrame without indexPrinting DataFrames in tabular formatRandomly splitting DataFrame into multiple DataFrames of equal sizeReducing DataFrame memory sizeSaving a DataFrame as a CSV fileSaving DataFrame as Excel fileSaving DataFrame as feather fileSetting all values to zeroShowing all dtypes without truncationSplitting DataFrame into multiple DataFrames based on valueSplitting DataFrame into smaller equal-sized DataFramesWriting DataFrame to SQLite
0
0
0
new
Converting DataFrame to a list of dictionaries in Pandas
Programming
chevron_rightPython
chevron_rightPandas
chevron_rightCookbooks
chevron_rightDataFrame Cookbooks
chevron_rightMiscellaneous Cookbook
schedule Mar 9, 2022
Last updated Python●Pandas
Tags tocTable of Contents
expand_more To convert a DataFrame to a list of dictionaries, use the DataFrame's to_dict(orient="records")
method.
Example
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,3],"B":[4,5]}, index=["a","b"])df
A Ba 2 4b 3 5
To convert df
to a list of dictionaries:
df.to_dict(orient="records")
[{'A': 2, 'B': 4}, {'A': 3, 'B': 5}]
Here, the returned value is a list of dictionaries with the following key-value pairs:
key
: column labelvalue
: value of the column in n-th row.
Basically each item in the list represents a row of the DataFrame.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...