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

431 Views Asked by At

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
2

There are 2 best solutions below

1
On BEST ANSWER

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
1
On

Looks like CODES column is string typed, not dictionary typed. You can confirm with:

type(df['CODES'].iloc[0])
# should get `str`

If so, try:

(df['CODES'].str.extractall('"(?P<CODES>[^"]*)"')  # extract pattern between " "
   .reset_index('match',drop=True)                 # drop the match number
   .join(df[['ID']])                               # join ID back
)

Output:

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