Add column with number of days between dates in DataFrame pandas
Assuming these were datetime columns (if they're not apply to_datetime
) you can just subtract them:
df['A'] = pd.to_datetime(df['A'])
df['B'] = pd.to_datetime(df['B'])
In [11]: df.dtypes # if already datetime64 you don't need to use to_datetime
Out[11]:
A datetime64[ns]
B datetime64[ns]
dtype: object
In [12]: df['A'] - df['B']
Out[12]:
one -58 days
two -26 days
dtype: timedelta64[ns]
In [13]: df['C'] = df['A'] - df['B']
In [14]: df
Out[14]:
A B C
one 2014-01-01 2014-02-28 -58 days
two 2014-02-03 2014-03-01 -26 days
Note: ensure you're using a new of pandas (e.g. 0.13.1), this may not work in older versions.
Pandas: Subtracting two date columns and the result being an integer
How about:
df_test['Difference'] = (df_test['First_Date'] - df_test['Second Date']).dt.days
This will return difference as int
if there are no missing values(NaT
) and float
if there is.
Pandas have a rich documentation on Time series / date functionality and Time deltas
How to get number of days between two dates using pandas
For performance you can subtract values without apply
for avoid loops use Series.rsub
for subtract from rigth side:
df['date'] = pd.to_datetime(df.date)
df['days'] = df['date'].rsub(pd.Timestamp('today')).dt.days
What working like:
df['days'] = (pd.Timestamp('today') - df['date']).dt.days
If want use your solution:
df['date'] = pd.to_datetime(df.date)
def calculate_days(date):
today = pd.Timestamp('today')
return (today - date).days
df['days'] = df['date'].apply(lambda x: calculate_days(x))
Or:
df['date'] = pd.to_datetime(df.date)
def calculate_days(date):
today = pd.Timestamp('today')
return (today - date)
df['days'] = df['date'].apply(lambda x: calculate_days(x)).dt.days
Pandas: using column of date to calculate number of days
You can try this:
def add_host_days(df):
df['host_days'] = (pd.Timestamp.now() - pd.to_datetime(df['host_since'], dayfirst=True)).dt.days
# If you original date fields have invalid dates and would like this number of days to be in integer:
df['host_days'] = df['host_days'].astype('Int64')
return df
Demo
Suppose you have a dataframe like this:
guest_id host_since
0 A0001 24/09/2008
1 A0002 25/09/2008
2 A0003 29/09/2008
3 A8788 20/05/2021
Then you run the code:
new_df = add_host_days(df)
Result:
print(new_df)
guest_id host_since host_days
0 A0001 24/09/2008 4629
1 A0002 25/09/2008 4628
2 A0003 29/09/2008 4624
3 A8788 20/05/2021 8
How to make new column with string value if it is between two dates? Pandas
It is very easy
import numpy as np
df['new']= np.select([df.date.between(date1, date2)], ['person a'], 'person b')
np.select method is very easy and you can read more about it.
Also you can use a for loop for this but it is not optimum solution
Python: get count of days between dates from two columns in DataFrame without using apply()
Don't use apply
:
df['c'] = (df['b'] - df['a']).dt.days
Python: Create new column that counts the days between current date and a lag date
I came up with the solution of using a for loop and zip function, to simply subtract each pair like so...
from datetime import datetime
import pandas as pd
dates = ['2012-08-01', '2012-08-15', '2012-09-01', '2012-08-15']
dates2 = ['2012-08-01', '2012-09-01', '2012-10-01', '2012-11-01']
diff = []
for i, x in zip(dates, dates2):
i = datetime.strptime(i, '%Y-%m-%d')
x = datetime.strptime(x, '%Y-%m-%d')
diff.append(i - x)
df = {'--col1--': dates, '--col2--': dates2, '--difference--': diff}
df = pd.DataFrame(df)
print(df)
Ouput:
--col1-- --col2-- --difference--
0 2012-08-01 2012-08-01 0 days
1 2012-08-15 2012-09-01 -17 days
2 2012-09-01 2012-10-01 -30 days
3 2012-08-15 2012-11-01 -78 days
Process finished with exit code 0
I hope that solves your problem.
Pandas - Number of Months Between Two Dates
Here is a very simple answer my friend:
df['nb_months'] = ((df.date2 - df.date1)/np.timedelta64(1, 'M'))
and now:
df['nb_months'] = df['nb_months'].astype(int)
Related Topics
How to Output to the Same Line Overwriting the Previous Line
Difference Between Pygame.Display.Update and Pygame.Display.Flip
Python Selenium: Wait Until Element Is Clickable - Not Working
Selenium Unable to Locate Element Only When Using Headless Chrome (Python)
Count Frequency of Values in Pandas Dataframe Column
What's the Best Way to Duplicate Fork() in Windows
Python Ungzipping Stream of Bytes
When Installing Pyaudio, Pip Cannot Find Portaudio.H in /Usr/Local/Include
Matplotlib Dateformatter for Axis Label Not Working
How to Use 'Cv2.Findcontours' in Different Opencv Versions
Overwriting File in Ziparchive
Cannot Use Geometry Manager Pack Inside
Pygame 2 Dimensional Movement of an Enemy Towards the Player, How to Calculate X and Y Velocity
Why Doesn't 2._Add_(3) Work in Python