gpt4 book ai didi

Python-如何在字符串中绘制日期和时间

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:29 25 4
gpt4 key购买 nike

我有这个数据框

    user_date   user_time
0 Thu 07:32:20
1 Wed 21:10:38
2 Fri 21:32:55
3 Sat 11:57:36
4 Fri 22:37:41

如何绘制此数据框,x 为 user_time,范围为 24 小时,y 为 user_date。

同时,非常感谢您的关注和参与。

最佳答案

user_date 值存在问题,因为如果使用string get:

TypeError: Empty 'DataFrame': no numeric data to plot

一种可能的解决方案是将它们转换为 ordered categorical并由 cat.codes 绘制.

还绘制timedelata未实现,因此 user_time 被转换为 to_datetime然后通过 strftime 转换为string :

days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
df['user_date'] = df['user_date'].astype('category', categories=days, ordered=True)

df['user_time1'] = pd.to_datetime(df['user_time']).dt.strftime('%H:%M:%S')
df['user_date1'] = df['user_date'].cat.codes

ax = df.plot(x='user_time1', y='user_date1')

#set max and min values of axis
#https://stackoverflow.com/a/43460067/2901002
ax.set_yticks(range(7))
ax.set_yticklabels(days)

graph

关于Python-如何在字符串中绘制日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44714898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com