获取时间序列中数据框的失败次数和成功次数

时间:2016-07-13 19:52:42

标签: python pandas dataframe

使用这样的dataFrame:

Time                    Status      ResponseTime     PID
2016-07-13 17:33:49     OK          1623             42
2016-07-13 17:33:50     KO          1593             35
2016-07-13 17:33:50     OK          1604             19
2016-07-13 17:33:51     KO          1605             28
2016-07-13 17:33:51     OK          1617             42
2016-07-13 17:33:52     OK          1654             35
2016-07-13 17:33:52     OK          2044             19
2016-07-13 17:33:53     KO          1630             42

如何绘制一种直方图,每分钟都能显示一些KO和OK?

1 个答案:

答案 0 :(得分:3)

text = """Time                    Status      ResponseTime     PID
2016-07-13 17:33:49     OK          1623             42
2016-07-13 17:33:50     KO          1593             35
2016-07-13 17:33:50     OK          1604             19
2016-07-13 17:33:51     KO          1605             28
2016-07-13 17:33:51     OK          1617             42
2016-07-13 17:33:52     OK          1654             35
2016-07-13 17:33:52     OK          2044             19
2016-07-13 17:33:53     KO          1630             42
2016-07-13 17:34:49     OK          1623             42
2016-07-13 17:34:50     KO          1593             35
2016-07-13 17:34:50     OK          1604             19
2016-07-13 17:34:51     KO          1605             28
2016-07-13 17:34:51     OK          1617             42
2016-07-13 17:34:52     OK          1654             35
2016-07-13 17:34:52     OK          2044             19
2016-07-13 17:34:53     KO          1630             42
"""

df = pd.read_csv(StringIO(text), sep='\s{2,}', engine='python', index_col=0, parse_dates=[0])

df1 = df.groupby(pd.TimeGrouper('Min')).Status.value_counts().unstack()
df1

enter image description here

df1.plot.bar()

enter image description here

如果您想限制为'KO'

df1.KO.plot.bar()

enter image description here