I am looking to apply a function to multiple columns to a datatable
in Python. With R
's data.table
one would:
# columns to apply function to
x <- c('col_1', 'col_2')
# apply
df[, (x) := lapply(.SD, function(x) as.Date(x, "%Y-%m-%d")), .SDcols=x]
How would one do the same using Python's datatable
? I have some knowledge of apply
and lambda
with pandas
e.g.:
# create dummy data
df = pd.DataFrame({'col_1': ['2021-12-01']
, 'col_2': ['2021-12-02']
, 'col_3': ['foobar']
}
)
# columns to apply function to
x = ['col_1', 'col_2']
# apply
df[x] = df[x].apply(lambda x: pd.to_datetime(x, format='%Y-%m-%d'))
but what is its equivalent in Python's datatable
? This is assuming I insist on the use of apply
and lambda
. Thank you.
edit* I have changed from an UDF to a standard function pd.to_datetime
as some of us mentioned the former is not possible while the latter is. Feel free to use any examples to illustrate apply
in conjunction with datatable
. Thank you