user6771469
user6771469

Reputation:

Pandas apply function using two columns from dataframe to create a new one

I have a simple function that counts weekends and holidays within two dates in python...

count_holiday_and_weekends(fromdate,todate)

How do i apply the function to create a new one in my df ?

Something like:

df['count_holiday_and_weekends'] = count_holiday_and_weekends(df['fromdate'],df['todate])

thanks in advance !

Upvotes: 3

Views: 419

Answers (2)

BENY
BENY

Reputation: 323226

If you know which columns to be used in the function you can do

df.assign(count_holiday_and_weekends=df[['fromdate','todate']].apply(count_holiday_and_weekends,axis=1))

Upvotes: 0

jezrael
jezrael

Reputation: 862641

Use apply with parameetr axis=1 for process by rows:

df['count_holiday_and_weekends'] = df.apply(lambda x: count_holiday_and_weekends(x['fromdate'],x['todate']), axis=1)

Upvotes: 5

Related Questions