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

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
user1708646
  • 111
  • 1
  • 6
  • A slightly safer/better way is to use [`literal_eval`](http://stackoverflow.com/a/3513475/1240268). I don't think you will be able to do this automatically... – Andy Hayden Jan 10 '13 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? – Andy Hayden Jan 10 '13 at 15:21

1 Answers1

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.

Wes McKinney
  • 101,437
  • 32
  • 142
  • 108