Search code examples
pythonpython-datetime

How to populate 2 columns in DataFrame with same "def" function?


I have a data frame that has a date column, what I need is to create another 2 columns with the "start of week date" and "end of week date". The reason for this is that I will then need to group by an "isoweek" column... but also keep this two-column "start_of_week_date" and "end_of_week_date"

I've created the below function:

def myfunc(dt, option):

wkday = dt.isoweekday()

if option == 'start':
    delta = datetime.timedelta(1 - wkday)
elif option == 'end':
    delta = datetime.timedelta(7 - wkday)
else:
    raise TypeError
    
return date + delta

Now I don't know how I would use the above function to populate the columns.

Probably don't even need my function to get what I need... which is... I have a DF that has the below columns

\>>> date, isoweek, qty

I will need to change it to:

\>>> isoweek, start_of_week_date, end_of_week_date, qty

this would then make my data go from 1.8 million rows to 300 thousand rows :D

can someone help me?

thank you


Solution

  • There might be builtin functions that one can use and i can see one of the answers proposes such.

    However, if you wish to apply your own function (which is perfectly acceptable) then could use the apply with lambda.

    Here is an example:

    import pandas as pd
    from datetime import datetime
    
    # an example dataframe
    d = {'some date':[1,2,3,4],
        'other data':[2,4,6,8]}
    df = pd.DataFrame(d)
    
    
    # user defined function from the question
    def myfunc(dt, option):
    
        wkday = dt.isoweekday()
    
        if option == 'start':
            delta = datetime.timedelta(1 - wkday)
        elif option == 'end':
            delta = datetime.timedelta(7 - wkday)
        else:
            raise TypeError
            
        return date + delta
    
    
    df['new_col'] = df.apply(lambda x: myfunc(df['some data'], df['other data']), axis=1)