I have data of string in a pandas dataframe column. I need to convert it to either parsable json string or dict type so that I can read / extract values from it.
Mock / sample DataFrame:
df = pd.DataFrame({'col1': [6010, 6015, 6020, 6025],
'json_col': ["{'Id': '060',
'Date': '20210908',
'value': {'Id': '060',
'Code': '06037'}
}",
"{'Id': '061',
'Date': '20210908',
'value': {'Id': '060',
'Code': '06038'}
}",
np.nan,
"{'Id': '063',
'Date': '20210908',
'value': {'Id': '060',
'Code': '06040'}
}"],
})
Here's what I tried:
df['json_col'] = df[df['json_col'].notnull()].map(lambda x: dict(eval(x)))
TypeError: eval() arg 1 must be a string, bytes or code object
Note, there are some missing values in the column and the function need to be able to handle them.