Python timeseries数据帧中的多天绘图
我的数据框是这样的-Python timeseries数据帧中的多天绘图,python,pandas,plot,time-series,Python,Pandas,Plot,Time Series,我的数据框是这样的- In [1]: df.head() Out[1]: Datetime Value 2018-04-21 14:08:30.761 offline 2018-04-21 14:08:40.761 offline 2018-04-21 14:08:50.761 offline 2018-04-21 14:09:00.761 offline 2018-04-21 14:09:10.761 offlin
In [1]: df.head()
Out[1]:
Datetime Value
2018-04-21 14:08:30.761 offline
2018-04-21 14:08:40.761 offline
2018-04-21 14:08:50.761 offline
2018-04-21 14:09:00.761 offline
2018-04-21 14:09:10.761 offline
我有两周的数据。我想绘制一周中每天的时间值(小时:分钟)。如果我要一周一次地查看数据,这也行得通
我用一天的时间拍摄了一个片段,然后用plotly创建了一个绘图
In[9]: df['numval'] = df.Value.apply(lambda x: 1 if x == 'online' else -1)
In[10]: df.iplot()
如果能有多个类似的情节,周日至周六使用几行,这将加快我的工作
建议-
我可以把参数放在工作日(0-6)、时间(x轴)和值(y轴)中,这样可以创建7个图
In[11]: df['weekday'] = df.index.weekday
In[12]: df['weekdayname'] = df.index.weekday_name
In[13]: df['time'] = df.index.time
任何库都可以工作,因为我只想查看数据,并且需要测试对数据的修改
可选-分布曲线,类似KDE,在数据上会很好
这可能不是您想要的确切答案。只是给出一个可能有用的方法 这里的方法是根据日期对数据进行分组,然后为每个组生成一个图。为此,您需要将DateTime列拆分为两列—date和time。下面的代码将执行此操作:
datetime_series = df['Datetime']
date_series = pd.Series()
time_series = pd.Series()
for datetime_string in datetime_series:
date,time = datetime_string.split(" ")
date_s = pd.Series(date,dtype=str)
time_s = pd.Series(time, dtype=str)
date_series=date_series.append(date_s, ignore_index=True)
time_series = time_series.append(time_s, ignore_index=True)
上面的代码将为您提供两个独立的熊猫系列。一个表示日期,另一个表示时间。现在,您可以将这两列添加到数据帧中
df['date'] = date_series
df['time'] = time_series
现在,您可以使用groupby功能根据日期对数据进行分组,并为每个组绘制数据。大概是这样的:
首先将“脱机”替换为值0:
df1 = df.replace(to_replace='offline',value=0)
现在根据日期和绘图对数据进行分组:
for title, group in df1.groupby('date'):
group.plot(x='time', y='Value', title=title)