Skip to content

Instantly share code, notes, and snippets.

@bruno-uy
Created December 31, 2021 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruno-uy/fe5a91b38bf83ac635ecf544e7457566 to your computer and use it in GitHub Desktop.
Save bruno-uy/fe5a91b38bf83ac635ecf544e7457566 to your computer and use it in GitHub Desktop.
Export pandas DataFrame to a dict with None instead of nan
import pandas as pd
import numpy as np
df = pd.DataFrame({"A": [1, 2, 3], "B": [1.2, np.NaN, 3.4]})
result = (
df
.replace([np.nan], [None], regex=False)
.to_dict(orient="records")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment