Last active
April 20, 2022 18:11
-
-
Save FerusAndBeyond/71b4db768c7aa6bd306a299192d32896 to your computer and use it in GitHub Desktop.
Dictionaries to pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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