2

I am starting to learn SFrames data structure lately. I wonder if anyone experienced the problem of converting a "str" column to "float" or "int" column. In R, I could easily accomplish the task by as.numeric() or as.integer().

How could I do that in SFrames (python)?

Thanks,

Ying Haw Lee
  • 99
  • 3
  • 6

2 Answers2

3

I've found this approach works.

sf['column_name'] = sf['column_name'].astype(float)
sf['column_name'] = sf['column_name'].astype(str)
Matt L.
  • 397
  • 4
  • 12
  • Hi @Matt how can we do this task while handling the errors, e.g. if a 'string' value is present in the column, and want to convert the column to an 'int' type. Ideal case would be to set these string values to '0'. – skadoosh Oct 09 '17 at 06:43
  • @skadoosh Honestly I'd handle that before you get to this point. I don't think the astype is very robust to those errors assuming I understand the issue correctly. – Matt L. Oct 10 '17 at 21:16
  • currently astype raises errors, but we do have an option to exclude errors, in which case, if a column value is as '3sab', it is converted to '3'(int), which is very weird. Also, I feel astype should be able to handle these errors, otherwise, why would I use astype in some cases. – skadoosh Oct 11 '17 at 06:40
0

This doesn't work sometimes specially for empty columns. Its better to assign first to either string or float and then load elements. Example

sf['column'] = ""  #for string
sf['column1'] = numpy.nan  #for float

and then load values. I got struck in similar situation.

Hari_pb
  • 7,088
  • 3
  • 45
  • 53