Reputation: 33
I am trying to execute:
df['C'] = df['C'].replace(0, lambda x: pd.Timedelta(x['B']-x['A']).days)
which is to replace all instances of zeros in column 'C' of dataframe 'df' with difference between column 'B' and 'A' which are of type datetime64. I want the result 'C' to be in days. Here is the snap of result after the above execution:
I am getting the column C values likes this. Instead i should get only the days which is the difference between the first two. What I am doin wrong? (note: I don't want to do df['C'] = df['B'] - df['A'] because i want to replace only zeros, not existing non-zeros)
Upvotes: 1
Views: 25
Reputation: 862581
Select columns by mask in DataFrame.loc
and set days created by Series.dt.days
:
df.loc[df['C'] == 0, 'C'] = (df['B']-df['A']).dt.days
Upvotes: 1