【问题标题】:Multiple day wise plots in timeseries dataframe pandas时间序列数据框熊猫中的多天明智图
【发布时间】:2018-12-15 14:44:52
【问题描述】:

我的数据框是这样的 -

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

我有 2 周的数据。我想为一周中的每一天绘制价值与时间(小时:分钟)的关系。如果我要每周查看数据,那也是可行的。

我花了一天时间用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,在数据上会很好

【问题讨论】:

    标签: python pandas plot time-series


    【解决方案1】:

    这可能不是您正在寻找的确切答案。只是提供一种可能会有所帮助的方法。

    这里的方法是根据日期对数据进行分组,然后为每个组生成一个图。为此,您需要将 DateTime 列拆分为两列 - 日期和时间。下面的代码将做到这一点:

    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 功能根据日期对数据进行分组,并为每个组绘制数据。像这样的:

    首先将 'offline' 替换为值 0:

    df1 = df.replace(to_replace='offline',value=0)
    

    现在根据日期和绘图对数据进行分组:

    for title, group in df1.groupby('date'):
        group.plot(x='time', y='Value', title=title)
    

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2021-05-27
      • 2020-09-08
      相关资源
      最近更新 更多