[英]How to convert string from csv to hour(s):minute(s)?
If I copied well the link, you two picture.如果我复制好链接,你们两张照片。 One shows the csv file I have and the other the graph I want to create.一个显示我拥有的 csv 文件,另一个显示我想要创建的图表。
It would be easy so there is a but,这很容易,所以有一个但是,
I want to represent the AVG number (which are seconds actually) as hour(s):minute(s) on y axis.我想在 y 轴上将 AVG 数(实际上是秒)表示为小时:分钟。
I think, it cannot be solved because I spent 3 days wit this problem.我认为,它无法解决,因为我花了 3 天时间解决这个问题。
Everything, in terms of my googling skills and etc...一切,就我的谷歌搜索技能等而言......
But to be more precise, aside of lot of conversations with dateime, timedelta, timestamp nothing worked.但更准确地说,除了与 dateime、timedelta、timestamp 的大量对话之外,没有任何效果。
Either the data could no be shown on y axis because it did not represent number like variable to plot or I've got not proper representation of the data.要么数据无法显示在 y 轴上,因为它不代表 plot 之类的数字,要么我没有正确表示数据。
I was trying to create a different approach, like convert the seconds calculating with divmod than put them on the top of the bars with annonate.我试图创建一种不同的方法,例如转换使用 divmod 计算的秒数,而不是使用 annonate 将它们放在条形图的顶部。 There are problem with this, still...这个有问题,还是...
Later I was thinking I have found the holy grail:后来我想我找到了圣杯:
I could not use this, I do not understand how should I create an acceptable datatype for this..我无法使用它,我不明白我应该如何为此创建可接受的数据类型..
Thanks for anyone who can help!!感谢任何可以提供帮助的人!
the example you show in the first image could be reproduced by using plot.bar
from pandas您在第一张图片中显示的示例可以使用plot.bar
中的 plot.bar 进行复制
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df["activity"] = ['run', 'swim', 'drive']
>>> df["avg"] = [86400,43200,21600]
>>> df
activity avg
0 run 86400
1 swim 43200
2 drive 21600
>>> df.plot.bar(x="activity")
<AxesSubplot: xlabel='activity'>
>>> plt.show()
To represent time transcurred for a certain number of seconds you can use fromtimestamp
and formatting strftime
but it might not be compatible with matplotlib, then using Timple
is something related, but graph could not be properly plotted or maybe it is needed to perform something like exploratory data analysis or apply a certain statistical procedure.要表示经过一定秒数的时间,您可以使用fromtimestamp
和格式化strftime
,但它可能与 matplotlib 不兼容,然后使用Timple
是相关的,但无法正确绘制图形或者可能需要执行类似探索性的操作数据分析或应用一定的统计程序。
>>> import datetime
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> import timple
>>>
>>> tmpl = timple.Timple()
>>> tmpl.enable()
>>> timedeltas = np.array([datetime.timedelta(seconds=(s)) for s in df["avg"]])
>>> timedeltas
array([datetime.timedelta(days=1), datetime.timedelta(seconds=43200),
datetime.timedelta(seconds=21600)], dtype=object)
>>> plt.plot(timedeltas, df["activity"])
[<matplotlib.lines.Line2D object at 0x0000026FAC3F5B40>]
>>> plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.