Add Multiple Columns to Pandas Dataframe from Function

EFL :

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 columns: hour, weekday, and weeknum.

def getH(t): #gives the hour
    return t.hour
def getW(d): #gives the week number
    return d.isocalendar()[1] 
def getD(d): #gives the weekday
    return d.weekday() # 0 for Monday, 6 for Sunday

mydf["hour"] = mydf.apply(lambda row:getH(row["mytime"]), axis=1)
mydf["weekday"] = mydf.apply(lambda row:getD(row["mydate"]), axis=1)
mydf["weeknum"] = mydf.apply(lambda row:getW(row["mydate"]), axis=1)

The snippet works, but it's not computationally efficient as it loops through the data frame at least three times. I would just like to know if there's a faster and/or more optimal way to do this. For example, using zip or merge? If, for example, I just create one function that returns three elements, how should I implement this? To illustrate, the function would be:

def getHWd(d,t):
    return t.hour, d.isocalendar()[1], d.weekday()
Zero :

Here's on approach to do it using one apply

Say, df is like

In [64]: df
Out[64]:
       mydate     mytime
0  2011-01-01 2011-11-14
1  2011-01-02 2011-11-15
2  2011-01-03 2011-11-16
3  2011-01-04 2011-11-17
4  2011-01-05 2011-11-18
5  2011-01-06 2011-11-19
6  2011-01-07 2011-11-20
7  2011-01-08 2011-11-21
8  2011-01-09 2011-11-22
9  2011-01-10 2011-11-23
10 2011-01-11 2011-11-24
11 2011-01-12 2011-11-25

We'll take the lambda function out to separate line for readability and define it like

In [65]: lambdafunc = lambda x: pd.Series([x['mytime'].hour,
                                           x['mydate'].isocalendar()[1],
                                           x['mydate'].weekday()])

And, apply and store the result to df[['hour', 'weekday', 'weeknum']]

In [66]: df[['hour', 'weekday', 'weeknum']] = df.apply(lambdafunc, axis=1)

And, the output is like

In [67]: df
Out[67]:
       mydate     mytime  hour  weekday  weeknum
0  2011-01-01 2011-11-14     0       52        5
1  2011-01-02 2011-11-15     0       52        6
2  2011-01-03 2011-11-16     0        1        0
3  2011-01-04 2011-11-17     0        1        1
4  2011-01-05 2011-11-18     0        1        2
5  2011-01-06 2011-11-19     0        1        3
6  2011-01-07 2011-11-20     0        1        4
7  2011-01-08 2011-11-21     0        1        5
8  2011-01-09 2011-11-22     0        1        6
9  2011-01-10 2011-11-23     0        2        0
10 2011-01-11 2011-11-24     0        2        1
11 2011-01-12 2011-11-25     0        2        2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

add multiple columns to pandas dataframe

Create multiple pandas DataFrame columns from applying a function with multiple returns

How to add multiple columns to a dataframe from a custom function in R

Create multiple columns in Pandas Dataframe from one function

Python pandas add multiple columns to dataframe with map

Add multiple empty columns to pandas DataFrame

Add multiple columns and values to a pandas dataframe

Add multiple columns to a Pandas dataframe quickly

Pandas - how to add multiple conditional columns to dataframe?

Updating multiple columns in pandas dataframe with function

Pandas apply function on dataframe over multiple columns

Pandas DataFrame apply function to multiple columns and output multiple columns

How to add/insert output of a function call that returns multiple fields, as new columns into Pandas dataframe?

How to add multiple columns and assigning into new column name by using function in pandas dataframe?

How to locate multiple returned values from a function into multiple columns of pandas dataframe?

Join on multiple columns from another pandas dataframe

lookup from multiple columns in pandas dataframe

Calculate mean of columns from multiple dataframe in pandas

Pandas: apply function that return multiple new columns over Pandas DataFrame

Pandas - Add Columns to a DataFrame Based in Dict from one of the Columns

pandas dataframe resample aggregate function use multiple columns with a customized function?

Select columns from pandas dataframe using multiple conditions on columns in Python

Pandas Dataframe - add columns to one dataframe from another

How to create multiple additional columns from dataframe and add to the same dataframe

Add a new column to a dataframe based on multiple columns from another dataframe

Subset pandas dataframe on multiple columns based on values from another dataframe

Add multiple columns to MultiIndex dataframe from multiple scalar values

Add a column to Pandas DataFrame with multiple lookups based on other columns

How to add multiple columns to pandas dataframe in one assignment?