acnguy
acnguy

Reputation: 15

Create a column with a function which variables come from other columns

I have df with two columns of date: Start and End columns. I have a function which can calculate business days between two dates (and doing other stuff). bday_special(start, end)! And End value has to not be empty (NaT) else we put 0 in Delay.

I need to create a third column with the result of the two passed in the function.

I was used to do:

df['Delay'] = pd.to_datetime(df['End']) - pd.to_datetime(df['Start'])

Now I just need something like that :

df["Delay"] = df[df["End"]!=np.NaT].apply(bday_special(df["Start"], df["End"]))

df["Delay"] = df.apply(lambda df: if df["End"] is not np.NaT , bday_special(df["Start"], df["End"]), else 0)

TypeError: Cannot convert input

Both above doesn't work... What is the appropriate syntax please?

I prefer to stick with pandas scheme instead of going throug lists

Upvotes: 0

Views: 61

Answers (1)

Amir saleem
Amir saleem

Reputation: 1496

Use apply with the axis=1 parameter

df["Delay"] = df.apply(lambda x: 0 if x.End is np.nan else bday_special(x.Start, x.End), axis=1) # axis=1 means iterate over rows

Upvotes: 2

Related Questions