I have a dataframe with multiple dates, such as '2019-05-01' and I want to substract it to get e.g. 4 (2019-09-01 - 2019-05-01). Those are two columns.
Asked
Active
Viewed 102 times
0
-
Are those 'strings' or datetime datatypes? – Scott Boston Sep 07 '21 at 13:14
-
See also https://stackoverflow.com/q/42822768/10197418 – FObersteiner Sep 07 '21 at 14:43
-
Do you use Pandas dataframe or PySpark dataframe? – ZygD Sep 08 '21 at 07:21
-
Those are datetime datatypes – Adam_Gie Sep 08 '21 at 17:25
-
I use Pandas dadaframe – Adam_Gie Sep 08 '21 at 17:25
1 Answers
0
df['delta'] = [ (x.year - y.year) * 12 + (x.month - y.month) for x, y in zip(df['date1'],df['date2'])]
-
You neither need a list comprehension nor zip, see the dupes I linked. – FObersteiner Sep 07 '21 at 14:44
-
I'm afraid that this solution doesn't work - it shows wrong numbers eg. differences between Jan and FEB shows 0 whereas my solution shows 1 – Adam_Gie Oct 12 '21 at 08:34