0

I have a df like so:

  firstdate                seconddate
0 2011-01-01 13:00:00    2011-01-01 13:00:00
1 2011-01-02 14:00:00    2011-01-01 11:00:00
2 2011-01-02 16:00:00    2011-01-02 13:00:00
3 2011-01-04 12:00:00    2011-01-03 15:00:00
...

Seconddate is always before firstdate. I want to compute the difference between firstdate and seconddate in number of days and make this a column, if firstdate and seconddate are the same day, difference=0, if seconddate is the day before firstdate, difference=1 and so on until a week. How would I do this?

Scott Adamson
  • 385
  • 1
  • 3
  • 10

1 Answers1

1
df['first'] = pd.to_datetime(df['first'])
df['second'] = pd.to_datetime(df['second'])

df['diff'] = (df['first'] - df['second']).dt.days

This will add a column with the diff. You can delete based on it

df.drop(df[df.diff < 0].index)

# or

df = df[df.diff > 0]
leoOrion
  • 1,833
  • 2
  • 26
  • 52