Search code examples
pythonpandasgeneric-lambda

Str object has no attribute 'keys' while using Lambda function in Pandas


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

Solution

  • You can use str.findall with regex pattern to extract all the occurrences of codes from the dict like string, then explode the dataframe:

    df.assign(CODES=df['CODES'].str.findall(r'"(\d+)"')).explode('CODES')
    

    Another idea is to use literal_eval to evaluate the strings in CODES column as python dictionaries, then explode the dataframe:

    from ast import literal_eval
    
    df.assign(CODES=df['CODES'].map(literal_eval).map(list)).explode('CODES')
    

      ID       CODES
    0  A  1407273790
    0  A  1801032636
    0  A  1174813554
    0  A  1215470448
    0  A  1053754655
    0  A  1891751228
    1  B  1497066526
    1  B  1639360563
    1  B  1235107087
    1  B  1033522925
    2  C  1154348191
    2  C  1568410355