【问题标题】:Extract Time from DateTime dataframe column and Plot hourly time with lineplot从 DateTime 数据框列中提取时间并使用线图绘制每小时时间
【发布时间】:2019-11-05 00:33:24
【问题描述】:

因为我有一个 DateTime 格式的列,并且想要绘制一个图表,该图表将显示每小时的时间间隔,以找出用户最活跃的时间。但是,我对如何在 x 轴上绘制带有提取时间和每小时时间间隔的 seaborn 线图有点困惑。任何帮助将不胜感激。

日期时间栏:

 2019-06-16 09:50:32.716000
 2019-06-16 09:50:32.716001
 2019-06-16 09:50:35.374002
 2019-06-16 09:50:36.032003
 2019-06-16 06:41:09.959000
 2019-06-16 07:04:59.880000
 2019-06-16 07:05:09.925000
 2019-06-16 07:05:18.680002
 2019-06-16 07:05:18.676003
 2019-06-16 07:06:04.646004
 2019-06-16 07:06:04.646005
 2019-06-16 07:06:25.354006
 2019-06-16 07:06:25.353007
 ....
 2019-06-16 07:06:28.464008
 2019-06-16 07:06:28.464009
 2019-06-16 07:06:37.777010
 2019-06-16 07:06:37.942011
 2019-06-16 07:07:05.512012
 2019-06-16 07:07:13.342013
 2019-06-16 05:15:38.923000
 2019-06-16 05:15:45.772001
 2019-06-16 05:15:51.937000
 2019-06-16 05:15:57.064003
 2019-06-16 05:15:57.064004
 2019-06-16 05:16:07.163005
 2019-06-16 05:16:07.162006

到目前为止的代码:

sns.lineplot(data=dfdays[0]['event_timestamp'].dt.hour, color="coral", 
label="line")

【问题讨论】:

标签: python matplotlib seaborn


【解决方案1】:

经过一番搜索,我找到了答案。也许这可以帮助其他人。

从数据帧中提取时间:

minutes=[]
hours=[]
time=[]
minutes=dfdays[0].event_timestamp.dt.minute
hours=dfdays[0].event_timestamp.dt.hour
for i in range(len(dfdays[0])):
 x=float(str(hour[i])+'.'+str(minutes[i]))
 time.append(x)

计算数组中的条目:

 x = np.array(time)
 unique, counts = np.unique(x, return_counts=True)

使用 Seaborn 进行绘图:

sns.lineplot(unique,counts,color="coral", label="line")
plt.xlabel('Hours')
plt.ylabel('Users count')
plt.tight_layout()
plt.show()

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-10-15
    • 2019-11-15
    • 2015-06-12
    • 2013-12-25
    • 2020-11-08
    相关资源
    最近更新 更多