0

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?

2
  • could you pls paste your saved text file here as well ? It look like that there is a parentheses in your file. Commented Oct 24, 2016 at 1:23
  • I edited my question. Commented Oct 24, 2016 at 1:38

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.