Home > OS >  How to calculate the days difference between all the dates of a dataframe column and a single data i
How to calculate the days difference between all the dates of a dataframe column and a single data i

Time:10-08

I would to calculate the days difference between all the days in the "last_review" column and 2018-08-01, and I want the output to be exact days, like if the observation is 2018-07-31, the output should be 2. And do this for every observation of the dataframe column. The output should be 48894 * 1

enter image description here

CodePudding user response:

You can it like so:

df['last_review'] = pd.to_datetime(df['last_review'])
df['num_days'] = pd.to_datetime("2019-08-01") - df['last_review']

Output:

  last_review  num_days
0  2018-10-19  286 days
1  2019-05-21   72 days
2  2011-03-28 3048 days

CodePudding user response:

You can use:

sub_date = datetime(2018,8,1)
df['last_review'] = pd.to_datetime(df['last_review'])
df['diff'] = (sub_date - df['last_review']).dt.days
  • Related