[英]Counting Cumulative Occurrences and Plotting Over Time in Pandas
Given a sample dataframe that looks like:给定一个示例数据框,如下所示:
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
I want to cumulatively count the number of occurrences of "Exists" and "Does Not Exist", not the Occurrence of "APPLES", and plot those two values versus time.我想累计计算“存在”和“不存在”的出现次数,而不是“苹果”的出现次数,并绘制这两个值与时间的关系图。 I have created the Occurrences, as shown below, but the time is not in ascending order.我已经创建了 Occurrences,如下所示,但时间不是按升序排列的。
Thank you.谢谢你。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.