I have DataFrame like below:
df = pd.DataFrame({"ID" : ["1", "2", "3"], "Date" : ["12/11/2020", "12/10/2020", "05/04/2020"]})
And I need to calculate number of MONTHS from Date column until today. Below I upload result which I need:
Advertisement
Answer
You can modify this solution for subtract by scalar d
:
df['Date'] = pd.to_datetime(df['Date'], dayfirst=True) d = pd.to_datetime('now') df['Amount'] = 12 * (d.year - df['Date'].dt.year) + d.month - df['Date'].dt.month print (df) ID Date Amount 0 1 2020-11-12 1 1 2 2020-10-12 2 2 3 2020-04-05 8