及时绘制pandas数据框

时间:2017-07-11 01:20:19

标签: python pandas matplotlib

我有一个数据框,白天在应用程序中包含错误:

date	        code	        server	                type
2017-06-20	ADFC-64001	nexteesadf_server1	WARNING
2017-06-20	ADFC-64001	nexteesadf_server1	WARNING
2017-06-20	ADFC-64003	nexteesadf_server1	WARNING
2017-06-20	ADFC-64003	nexteesadf_server1	WARNING
2017-06-20	ADFC-64002	nexteesadf_server1	WARNING
2017-06-20	ADFC-64001	nexteesadf_server1	WARNING
2017-06-20	ADFC-64003	nexteesadf_server1	WARNING
2017-06-20	ADFC-64003	nexteesadf_server1	WARNING
2017-06-20	ADFC-64002	nexteesadf_server1	WARNING
2017-06-20	ADFC-64001	nexteesadf_server1	WARNING

我现在想要将每天每个错误的#次出现作为折线图及时绘制。因此,ADFC-64001将有1行,ADFC-64002将有1行,等等。

这样做的最佳方式是什么?

提前致谢。

1 个答案:

答案 0 :(得分:3)

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
print(df.groupby('code').resample('D').count().type.unstack('date').fillna(0))

date        2017-06-20  2017-06-21  2017-06-22  2017-06-23  2017-06-25
code                                                                  
ADFC-64001         4.0         1.0         0.0         0.0         1.0
ADFC-64002         2.0         0.0         0.0         1.0         0.0
ADFC-64003         3.0         2.0         1.0         0.0         0.0

数据

          date        code              server     type
0   2017-06-20  ADFC-64001  nexteesadf_server1  WARNING
1   2017-06-20  ADFC-64001  nexteesadf_server1  WARNING
2   2017-06-20  ADFC-64003  nexteesadf_server1  WARNING
3   2017-06-20  ADFC-64003  nexteesadf_server1  WARNING
4   2017-06-20  ADFC-64002  nexteesadf_server1  WARNING
5   2017-06-20  ADFC-64001  nexteesadf_server1  WARNING
6   2017-06-20  ADFC-64003  nexteesadf_server1  WARNING
7   2017-06-21  ADFC-64003  nexteesadf_server1  WARNING
8   2017-06-20  ADFC-64002  nexteesadf_server1  WARNING
9   2017-06-20  ADFC-64001  nexteesadf_server1  WARNING
10  2017-06-21  ADFC-64001  nexteesadf_server1  WARNING
11  2017-06-21  ADFC-64003  nexteesadf_server1  WARNING
12  2017-06-22  ADFC-64003  nexteesadf_server1  WARNING
13  2017-06-23  ADFC-64002  nexteesadf_server1  WARNING
14  2017-06-25  ADFC-64001  nexteesadf_server1  WARNING

import matplotlib.pyplot as plt

plt.style.use('ggplot')
df.groupby('code').resample('D').count().type.unstack('date').fillna(0).T.plot()
plt.show()

enter image description here