Pandas Find Duration Between Dates Where A Condition Is Met?
I have a pandas DataFrame that looks like this: ╔═══╦════════════╦═════════════╗ ║ ║ VENDOR ID ║ DATE
Solution 1:
I get a bit different output:
df['DATE'] = pd.to_datetime(df['DATE'])
df['GAP'] = df.groupby('VENDOR ID')['DATE'].diff().dt.days
print (df)
VENDOR ID DATE GAP
1 33 2018-01-12 NaN
2 33 2018-03-12 59.0
3 12 2018-01-08 NaN
4 12 2018-01-15 7.0
5 12 2018-01-23 8.0
6 33 2018-05-12 61.0
7 89 2018-01-12 NaN
Explanation:
- Convert column
to_datetime
- Then
groupby
withdiff
- Last convert
timedeltas
s todays
Post a Comment for "Pandas Find Duration Between Dates Where A Condition Is Met?"