pythonbeginner
DataFrame to Dict Records
Convert DataFrames to lists of dicts for API responses, JSON export, or further processing.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import json
df = pd.DataFrame({'id':[1,2,3],'name':['Alice','Bob','Carol'],'score':[90.5,85.0,92.3]})
# List of dicts
records = df.to_dict(orient='records')
print(records)
# JSON string
json_str = df.to_json(orient='records', indent=2)
print(json_str)
# Index-keyed dict
id_map = df.set_index('id').to_dict(orient='index')
print(id_map)Use Cases
- API serialization
- JSON export
- dict conversion
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonbeginner
Flatten Nested JSON with pandas
Use pd.json_normalize to flatten deeply nested API responses into a flat DataFrame.
Best for: API response flattening
#pandas#json
pythonbeginner
Fast JSON Serialisation with orjson
Use orjson for 5-10x faster JSON serialisation of large Python dicts, dataclasses, and NumPy arrays.
Best for: high-throughput serialisation
#orjson#json
pythonbeginner
Expand JSON Column into DataFrame Columns
Parse a JSON-string column and expand its keys into separate columns in one step.
Best for: JSON column expansion
#pandas#json
javaintermediate
Jackson — JSON Serialization and Parsing
Parse and generate JSON with Jackson: ObjectMapper, annotations, custom serializers, and streaming.
Best for: REST API request and response serialization
#java#json