Add Multiple Columns to Pandas Dataframe from Function

后端 未结 4 1087
执笔经年
执笔经年 2021-01-30 10:41

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

4条回答
  •  滥情空心
    2021-01-30 11:31

    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
    

提交回复
热议问题