I have a pandas dataframe looking like this:
Name start end A 2000-01-10 1970-04-29
I want to add a new column providing the difference between the start
and end
column in years, months, days.
So the result should look like:
Name start end diff A 2000-01-10 1970-04-29 29y9m etc.
the diff column may also be a datetime
object or a timedelta
object, but the key point for me is, that I can easily get the Year and Month out of it.
What I tried until now is:
df['diff'] = df['end'] - df['start']
This results in the new column containing 10848 days
. However, I do not know how to convert the days to 29y9m etc.
Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.
To calculate time difference between two Python Pandas columns in hours and minutes, we can subtract the datetime objects directly. We create a Panda DataFrame with 3 columns. Then we set the values of the to and fr columns to Pandas timestamps.
Pandas has various functions to create a date series. You can use the date_range method for timestamps, the period_range method for the period, and the timedelta_range method for time delta data. The date_range method is used to get a fixed frequency DatetimeIndex.
You can try by creating a new column with years in this way:
df['diff_year'] = df['diff'] / np.timedelta64(1, 'Y')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With