user36729 user36729 - 1 month ago 8
Python Question

Saving a data frame with a column of list in python

I have a data frame a below:

PT CA DE AP
0 1 2 [3,4,5,6]
1 4 6 [7,8,9]


when I save this data frame and read it again, the 'AP' column is stored as string:

PT CA DE AP
0 1 2 '(3,4,5,6)'
1 4 6 '(7,8,9)'


When I try to change it to integer by the following command, I got an error:

df.AP = df.apply(lambda r: [int(r.AP[j]) for j in range(len(r.AP))], axis = 1)


Error:

("invalid literal for int() with base 10: '('", 'occurred at index 0')


Could you please tell me how can I solve this issue?

Answer

Your AP column is tuple quoted in a string, so can't convert to list directly. Try this:

ap = r.AP.replace('(', '').replace(')', '').split(',')
df.AP = df.apply(lambda r: [int(ap[j]) for j in range(len(ap)], axis = 1)