Pandas 使用重采样()python绘制情绪分析输出的时间序列图

Pandas 使用重采样()python绘制情绪分析输出的时间序列图,pandas,time-series,sentiment-analysis,Pandas,Time Series,Sentiment Analysis,我有一组twitter提要的时间戳,它们各自的输出值存储在csv中。我需要将情绪值与6个小时周期进行聚合,并绘制一个时间序列图。请帮助,我正在尝试使用pandas中的重采样()来完成此操作 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

我有一组twitter提要的时间戳,它们各自的输出值存储在csv中。我需要将情绪值与6个小时周期进行聚合,并绘制一个时间序列图。请帮助,我正在尝试使用pandas中的重采样()来完成此操作

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\u csv('dataset.csv',name=['date',score'],index\u col=['date'],parse\u dates=['date'])
您可以使用
rolling
对于此用例,请参阅文档。另外,请查看以了解有关重采样与滚动的更多详细信息

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()

注释

  • 第页给出了可用于滚动窗口大小的偏移列表
  • 我使用了上一个API,以前的专用方法必须用于每个统计数据,例如
    rolling\u mean
    for mean