Skip to content

Instantly share code, notes, and snippets.

@FerusAndBeyond
Last active April 20, 2022 18:11
Show Gist options
  • Save FerusAndBeyond/71b4db768c7aa6bd306a299192d32896 to your computer and use it in GitHub Desktop.
Save FerusAndBeyond/71b4db768c7aa6bd306a299192d32896 to your computer and use it in GitHub Desktop.
Dictionaries to pandas
import pandas as pd
# list of dicts to pd.DataFrame
df = pd.DataFrame([
{ "a": 5, "b": 6 },
{ "a": 6, "b": 7 }
])
# df =
# a b
# 0 5 6
# 1 6 7
# to go back to list of dicts
a = df.to_dict(orient="records")
# a = [
# { "a": 5, "b": 6 },
# { "a": 6, "b": 7 }
# ]
# a dict to pd.Series
srs = pd.Series({ "a": 5, "b": 6 })
# srs =
# a 5
# b 6
# dtype: int64
# to go back to dictionary
a = srs.to_dict()
# a = {'a': 5, 'b': 6}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment