I have a pandas data frame mydf
that has two columns,and both columns are datetime datatypes: mydate
and mytime
. I want to add three more
To complement John Galt's answer:
Depending on the task that is performed by lambdafunc
, you may experience some speedup by storing the result of apply
in a new DataFrame
and then joining with the original:
lambdafunc = lambda x: pd.Series([x['mytime'].hour,
x['mydate'].isocalendar()[1],
x['mydate'].weekday()])
newcols = df.apply(lambdafunc, axis=1)
newcols.columns = ['hour', 'weekday', 'weeknum']
newdf = df.join(newcols)
Even if you do not see a speed improvement, I would recommend using the join
. You will be able to avoid the (always annoying) SettingWithCopyWarning
that may pop up when assigning directly on the columns:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead