Reputation: 575
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?
Upvotes: 0
Views: 224
Reputation: 4569
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)
Upvotes: 1