Pandas find duration between dates where a condition is met?

前端 未结 1 689
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 05:52

I have a pandas DataFrame that looks like this:

╔═══╦════════════╦═════════════╗
║   ║ VENDOR ID  ║ DATE        ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 33               


        
相关标签:
1条回答
  • 2021-01-22 06:17

    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:

    1. Convert column to_datetime
    2. Then groupby with diff
    3. Last convert timedeltass to days
    0 讨论(0)
提交回复
热议问题