-2

I have two dates column, and i need a thirdcolumn which gives the days(integer like 2,3) between first two columns.

I just implemented Date2-Date1 which gave me this output.

Date1      |  Date2     | Days
2017-10-24 | 2017-10-25 | 1 days
2017-11-24 | 2017-11-29 | 4 days

Days column is of timedelta64[ns] type .

How to get rid of days keyword in Days column so that i get just an integer in Days column.

jpp
  • 159,742
  • 34
  • 281
  • 339
Mighty
  • 447
  • 1
  • 5
  • 13

1 Answers1

0

Use pd.Series.dt.days directly:

import pandas as pd

df = pd.DataFrame({'Date1': ['2017-10-24', '2017-11-24'],
                   'Date2': ['2017-10-25', '2017-11-29']})

df['Date1'] = pd.to_datetime(df['Date1'])
df['Date2'] = pd.to_datetime(df['Date2'])

df['Days'] = (df['Date2'] - df['Date1']).dt.days

print(df)

       Date1      Date2  Days
0 2017-10-24 2017-10-25     1
1 2017-11-24 2017-11-29     5
jpp
  • 159,742
  • 34
  • 281
  • 339