【发布时间】:2017-09-29 19:55:27
【问题描述】:
我想使用 python 的 pandas 模块(参见下面的链接)通过时间序列表示来可视化 CSV 数据,如下所示。
df1的样本数据:
TIMESTAMP eventid
0 2017-03-20 02:38:24 1
1 2017-03-21 05:59:41 1
2 2017-03-23 12:59:58 1
3 2017-03-24 01:00:07 1
4 2017-03-27 03:00:13 1
“eventid”列始终包含值 1,我试图显示数据集中每一天的事件总和。是
pandas.Series.cumsum()
用于此目的的正确函数?
到目前为止的脚本:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df1 = pd.read_csv('timestamp01.csv')
print df1.columns # u'TIMESTAMP', u'eventid'
# I: ts = pd.Series(df1['eventid'], index=df1['TIMESTAMP'])
# O: Blank plot
# I: ts = pd.Series(df1['eventid'], index=pd.date_range(df1['TIMESTAMP'], periods=1000))
# O: TypeError: Cannot convert input ... Name: TIMESTAMP, dtype: object] of type <class 'pandas.core.series.Series'> to Timestamp
# working test example:
# I: ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
# O: See first link below (first plot).
ts = ts.cumsum()
ts.plot()
plt.show()
我尝试过的链接:
http://pandas.pydata.org/pandas-docs/stable/visualization.html
Aggregating timeseries from sensors
(上面的例子有不同的值,而不是我的 'eventid' 数据)
非常感谢任何帮助。
【问题讨论】:
标签: python pandas matplotlib dataframe time-series