I get the errors - 'str' object has no attribute 'keys' while running the lambda codes to explode.
ID CODES
A {"1407273790":5,"1801032636":20,"1174813554":1,"1215470448":2,"1053754655":4,"1891751228":1}
B {"1497066526":19,"1639360563":16,"1235107087":11,"1033522925":18}
C {"1154348191":8,"1568410355":4}
I am using the following codes -
df['CODES'] = df['CODES'].apply(lambda x: [*x.keys()]) # or lambda x: list(x.keys()))
df = df.explode('CODES')
df
I get the errors - 'str' object has no attribute 'keys'
To get this -
ID CODES
A 1407273790
A 1801032636
A 1174813554
A 1215470448
A 1053754655
A 1891751228
B 1497066526
B 1639360563
B 1235107087
B 1033522925
C 1154348191
C 1568410355
You can use
str.findall
with regex pattern to extract all the occurrences of codes from the dict like string, thenexplode
the dataframe:Another idea is to use
literal_eval
to evaluate the strings inCODES
column as python dictionaries, thenexplode
the dataframe: