1

I'm carrying some mathematical operations on datatable fields as follows

Sample DT:

py_DT = dt.Frame({'junction' : ['BroadwayCycleTrack-N','BroadwayCycleTrack-N',
                 'Burke Gilman Trail','Burke Gilman Trail','Elliot Bay','Elliot Bay'],
                 'time_window' : ['N','MD','M','M','N','MD'],
                 'total_bus' :[3526,8732,6732,9821,6473,9273] 
                 })

I would like to calculate a percentage of busses passed thorough by each junction as

Code chunk

py_DT[:,{'perc_of': f.total_bus/dt.sum(f.total_bus)},by(f.junction)]

Here a new field with a name perc_of holds the calculated value in floating numbers such as

0.287649 and 0.712351 for crossing BroadwayCycleTrack-N

Here i was trying to make use of dt.math module to get the floating numbers rounded since the F expression in DT is not allowing python's round method.

Code chunks:

py_DT[:,{'perc_of': dt.math.trunc(f.total_bus/dt.sum(f.total_bus))},by(f.junction)]

py_DT[:,{'perc_of': dt.math.floor(f.total_bus/dt.sum(f.total_bus))},by(f.junction)]

py_DT[:,{'perc_of': dt.math.ceil(f.total_bus/dt.sum(f.total_bus))},by(f.junction)]

py_DT[:,{'perc_of': dt.math.trunc(f.total_bus/dt.sum(f.total_bus))},by(f.junction)]

They are not getting worked out to round off 0.287649 to 0.28 or 0.287, is there any other function i should try for it?. i was searching for it documentation but i couldn't find an appropriate one for it.

Pasha
  • 6,298
  • 2
  • 22
  • 34
myamulla_ciencia
  • 1,282
  • 1
  • 8
  • 30

1 Answers1

1

The dt.math.rint() function (https://datatable.readthedocs.io/en/latest/api/math.html#rint) rounds to the nearest integer.

There is no direct equivalent of python's round() function, but you are welcome to make a Feature Request at https://github.com/h2oai/datatable/issues to get it implemented.

For now, rounding to a specific number of digits can be achieved by multiplying a column by a power of 10, then rounding, then dividing again:

dt.math.rint(f[0] * 1000) / 1000
Pasha
  • 6,298
  • 2
  • 22
  • 34