Skip to content
Advertisement

Pandas: using column of date to calculate number of days

I am using an AirBnb dataset. I have a column, ‘host_since’. The column contains date objects in the format of ‘DD/MM/YYYY’: for example, 24/09/2008. The columns’ data shows the date that an individual became a host.

I want to create a new column in my dataframe that contains the the number of days since the host first joined. I am aware of the to_datetime method but cant quite understand the documentation.

note: df[‘host_days’] does not exist in the df. I want to create a column then assign the appropriate steps to get my outcome

JavaScript

Any ideas on how to do so? Thank you for your input.

Advertisement

Answer

You can try this:

JavaScript

Demo

Suppose you have a dataframe like this:

JavaScript

Then you run the code:

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement