2

I use a lot of tuples in my data files as record keys. When I load a table from text the tuples are strings in the dataframe.

I convert to tuple now, using:

df['KEY'] = df['KEY'].map(lambda x: eval(x))

Is it possible to get the string evaluation automatically?

Luc

2
  • A slightly safer/better way is to use literal_eval. I don't think you will be able to do this automatically... Commented Jan 10, 2013 at 14:49
  • Are these all of the same length? Does it make sense to include each item in the tuple as a separate column? Commented Jan 10, 2013 at 15:21

1 Answer 1

4

You could use converters={'KEY': eval} to make things a bit simpler. It would be possible to add some more optimized version of this internally, too.

Sign up to request clarification or add additional context in comments.

2 Comments

The suggestion of Wes does the trick, thanks, I will need this in almost all my pandas work...
How do we use converters={'KEY': eval} in the code you have mentioned in the question.

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.