Home > other >  Pandas Dataframe to Json using df.to_dict('records') changes a column on lists to strings
Pandas Dataframe to Json using df.to_dict('records') changes a column on lists to strings

Time:05-03

I want to convert a pandas dataframe to json format list of dictionaries using df.to_dict('records'). The dataframe contains a column of list of strings. When calling to_dict the list of strings are converted to a string e.g.

key1 key2
['a', 'b', 'c'] x

becomes...

[{"key1": "['a', 'b', 'c']", "key2": "x"}]

I would like the result to be

[{"key1": ['a', 'b', 'c'], , "key2": "x"}]

How do I prevent key1's value from being a sting?

CodePudding user response:

If you can convert the JSON into something like [{"key: ['a', 'b', 'c']"}] instead of {"key": "['a', 'b', 'c']"} you would be able to retain the JSON format.

data = ["key: ['a', 'b', 'c']"]

df = pd.DataFrame(data, columns = ["JSON"])
df
JSON
0 key: ['a', 'b', 'c']

Please share the original JSON output so that I can help you better.

CodePudding user response:

I am able to loop through the list of dictionaries and change the format using ast.literal_eval()

import ast

#where json is a list of dictionaries

for x in json:
    x["key_of_sting_of_list"] = ast.literal_eval(x["key_of_sting_of_list"])
  • Related