DEV Community

Murat Sert
Murat Sert

Posted on

Convert Pandas DataFrame column into dict

JSON_QUERY results are casted into strings encoded JSON objects when you perform a read_sql operation and load data into a DataFrame. If you wanted to preserve the column structure, you can use the following single line to convert it into a Python Dictionary.

Don't forget to
import json :)

df["column"] = df["column"].apply(lambda row: json.loads(row) if row else {})

Simply converts the "column" into a dictionary if the row exists, else empty dictionary.,

Top comments (0)