How to change a string containing a dict in pandas?

Question:

ID computed_data
0987 "{"Status":{"participate":14,"create":"10","activ":"0"},"rescount":22,"comcount":0,"partrate":0}"
4568 "{"Status":{"participate":49,"create":"40","activ":"27"},"rescount":22,"comcount":0,"partrate":73.47}"
1234 "{"Status":{"participate":3,"create":"3","activ":"1"},"comcount":0,"partrate":100,"rescount":42}"

I am trying to access and get the values in the computed_data column. It works on one cell when I am using eval().

eval(df["computed_data][0])

I tried a for loop to change all values at once and stored every dict in a list :

data = []
for x, i in enumerate(df["Computed Data"]):
    data.append(eval(df["Computed Data"][x]))

But I got an error "name "null" is not defined". I checked and I have no null values in my df which shape is 3601.

does anyone has an idea ? Thank you

Asked By: Koutaax

||

Answers:

Here is solution with custom function for convert not parseable strings to empty dictionaries:

import ast

def f(x):
    try:
        return ast.literal_eval(x)
    except ValueError:
        return {}
        
df["Computed Data"] = df[ "Computed Data"].str.strip('"').apply(f)
Answered By: jezrael

You can use ast.literal_eval after removing the external " if any (your string would otherwise be an invalid input):

from ast import literal_eval

df['computed_data'] = df['computed_data'].str.strip('"').apply(literal_eval)

Output:

     ID                                                                                                     computed_data
0   987       {'Status': {'participate': 14, 'create': '10', 'activ': '0'}, 'rescount': 22, 'comcount': 0, 'partrate': 0}
1  4568  {'Status': {'participate': 49, 'create': '40', 'activ': '27'}, 'rescount': 22, 'comcount': 0, 'partrate': 73.47}
2  1234       {'Status': {'participate': 3, 'create': '3', 'activ': '1'}, 'comcount': 0, 'partrate': 100, 'rescount': 42}
Answered By: mozway
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.