Python 计算大熊猫中的累积事件并绘制随时间变化的图

Python 计算大熊猫中的累积事件并绘制随时间变化的图,python,pandas,Python,Pandas,给定一个如下所示的示例数据帧: Time Type 2019-12-09 04:50 Exists 2019-12-08 01:20 Does Not Exist 2019-12-08 03:32 Exists 2019-12-07 01:15 APPLES 2019-12-05 04:13 Does Not Exist 我想累计计算“存在”和“不存在”的出现次数,而不是“苹果”的出现次数,并绘制这两个值随时间的变化曲线。我已经创建了事件,如下所示,但时间不

给定一个如下所示的示例数据帧:

Time              Type
2019-12-09 04:50  Exists
2019-12-08 01:20  Does Not Exist
2019-12-08 03:32  Exists
2019-12-07 01:15  APPLES
2019-12-05 04:13  Does Not Exist
我想累计计算“存在”和“不存在”的出现次数,而不是“苹果”的出现次数,并绘制这两个值随时间的变化曲线。我已经创建了事件,如下所示,但时间不是按升序排列的

  • 如何将时间更改为升序,然后仅在散点线图中绘制“存在”和“不存在”
  • 多谢各位

    import pandas as pd
    
    my_cols = ["Time","Type"]
    df = pd.read_csv('occurrences.txt',names = my_cols,sep=';')
    df['Time'] = pd.to_datetime(df['Time'])
    df.set_index('Time',inplace=True)
    df['Occurrence'] = df.groupby("Type").cumcount()
    

    首先过滤df并对值进行排序:

    new = df.loc[df['Type'].ne("APPLES")].sort_values(["Type","Time"])
    
    new["occurance"] = new.groupby("Type").cumcount()
    new.set_index("Time").groupby('Type')['occurance'].plot(legend=True)
    plt.show()
    

    绘图应该是什么样子?两条分开的线,一条表示存在,一条表示不存在?是的,这就是我想要的:X轴上的时间,Y轴上的累计发生率。