pythonrdata.tablepy-datatable

Data.table in R into datatable in Python translation problem (normalization)


I have this code in R where I am using data.table, and I have the intention to translate it into Python with datatable. It creates columns with the value of each existing column divided by the mean of the total. Kind of normalization.

dataset[ , paste0( cols, suffix) := lapply( .SD,  function(x){ x/mean(x, na.rm=TRUE)} ), 
         by= col_A, 
         .SDcols= cols]

Solution

  • from datatable import f,by,update,dt
    
    dataset=dt.Frame({'col_A':[0,0,1,1], 'col_B':[1,2,3,4], 'col_C':[5,6,7,8]})
    cols = dataset[:,[int,float]].names
    dataset[:, update(**{col+'_norm': f[col]/dt.mean(f[col]) for col in cols if col!='col_A'}), by(f.col_A)]