繁体   English   中英

计算 Pandas 中的累积发生次数并绘制随时间变化的图

[英]Counting Cumulative Occurrences and Plotting Over Time in 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

我想累计计算“存在”和“不存在”的出现次数,而不是“苹果”的出现次数,并绘制这两个值与时间的关系图。 我已经创建了 Occurrences,如下所示,但时间不是按升序排列的。

  1. 如何将时间更改为升序,然后仅在散点图中绘制“存在”和“不存在”?

谢谢你。

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 和sort_values

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()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM