python - CSV 数据的时间序列(时间戳和事件)

标签 python pandas matplotlib dataframe time-series

我想使用 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”数据相反)

d3: timeseries from data

非常感谢任何帮助。

最佳答案

看来您需要通过 read_csv 中的参数 parse_datesTIMESTAMP 列转换为 datetime :

import pandas as pd
from pandas.compat import StringIO

temp=u"""TIMESTAMP,eventid
2017-03-20 02:38:24,1
2017-03-20 05:38:24,1
2017-03-21 05:59:41,1
2017-03-23 12:59:58,1
2017-03-24 01:00:07,1
2017-03-27 03:00:13,1"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),  parse_dates=True, index_col='TIMESTAMP')
print (df)
                     eventid
TIMESTAMP                   
2017-03-20 02:38:24        1
2017-03-20 05:38:24        1
2017-03-21 05:59:41        1
2017-03-23 12:59:58        1
2017-03-24 01:00:07        1
2017-03-27 03:00:13        1

print (df.index)
DatetimeIndex(['2017-03-20 02:38:24', '2017-03-20 05:38:24',
               '2017-03-21 05:59:41', '2017-03-23 12:59:58',
               '2017-03-24 01:00:07', '2017-03-27 03:00:13'],
              dtype='datetime64[ns]', name='TIMESTAMP', freq=None)

然后使用resampledays 计算并按 size 计算功能。最后 Series.plot :

print (df.resample('D').size())
TIMESTAMP
2017-03-20    2
2017-03-21    1
2017-03-22    0
2017-03-23    1
2017-03-24    1
2017-03-25    0
2017-03-26    0
2017-03-27    1
Freq: D, dtype: int64

df.resample('D').size().plot()

如果需要改变代码的格式:

import matplotlib.ticker as ticker

ax = df.resample('D').size().plot()
ax.xaxis.set_major_formatter(ticker.FixedFormatter(df.index.strftime('%Y-%m-%d')))

关于python - CSV 数据的时间序列(时间戳和事件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735396/

相关文章:

python - GAE组织数据结构问题

python - 在 Python 中生成和压缩两个列表的最干净有效的方法

python - Pandas:drop_duplicates() 基于 python 中的条件

python - 使用循环 Python 绘制重复值

python - matplotlib 循环绘图,删除颜色条但保留空白

python - 高效的连接分组算法或python实现

python - Matplotlib png 输出在 reportlab 的 pdf 中出现 "broken"

python - 如何使用 python 运行 SQL 命令?

python - 使用 matplotlib 的大量子图

python - 如何在 matplotlib 中的每个子图上嵌入图像?