Subtracting Dates to get Days in pandas

前端 未结 1 931
甜味超标
甜味超标 2021-01-27 20:51

I have 2 columns date_1 (dtype(\'O\')) and date_2(dtype(\')

date_1            date_2
2018-06-06        2018-04-01

when i do subtra

1条回答
  •  鱼传尺愫
    2021-01-27 21:24

    Need convert columns to datetimes:

    df['date_1'] = pd.to_datetime(df['date_1'])
    df['date_2'] = pd.to_datetime(df['date_2'])
    

    Or:

    df = df.apply(pd.to_datetime)
    

    And then:

    df['days'] = (df['date_1'] - df['date_2']).dt.days
    

    0 讨论(0)
提交回复
热议问题