2016-11-18 30 views
2

我有一组时间戳,用于存储在csv中的twitter feeds和它们各自的输出情绪值。我需要将情绪值汇总为6小时的时间段并绘制时间序列图。请帮助,我正在尝试在熊猫中使用resample()。使用resample()的情绪分析输出的TimeSeries图形python

Sat Oct 01 00:43:02 +0000 2016,-0.5 
Sat Oct 01 00:43:18 +0000 2016,0 
Sat Oct 01 00:43:41 +0000 2016,-1 
Sat Oct 01 00:43:54 +0000 2016,-0.5 
Sat Oct 01 00:43:56 +0000 2016,-0.5 

df = pd.read_csv('dataset.csv', names=['date', 'score'], index_col=['date'],parse_dates=['date'])

回答

1

您可以使用rolling对于这种使用情况,请参阅文档here。请查看dedicated documentation了解更多关于重采样与滚动的细节。

import io 
import pandas as pd 

# Some test data 
zz = """date, value 
"Sat Oct 01 00:43:02 +0000 2016",-0.5 
"Sat Oct 01 05:43:18 +0000 2016",0 
"Sat Oct 01 11:43:41 +0000 2016",-1 
"Sat Oct 01 20:43:54 +0000 2016",-0.5 
"Sat Oct 01 23:43:56 +0000 2016",-0.5 
""" 

# Preparing the data Frame 
df = pd.read_table(io.StringIO(zz), delimiter=',') 
df['date'] = pd.to_datetime(df['date']) 
df.set_index('date', inplace=True) 

# Resampling with rolling window with a mean 
df.rolling('6H').mean().plot() 

enter image description here

  • 可用于滚动窗口大小偏移的列表在this页给出。
  • 我已经使用了最后一个API,以前专用的方法必须用于每个属性,例如rolling_mean的意思。