简体   繁体   English

如何将字符串从 csv 转换为小时:分钟?

[英]How to convert string from csv to hour(s):minute(s)?

text 文本

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:后来我想我找到了圣杯:

text文本

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()

使用 timple lib 绘图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有小时/分钟字符串的 CSV 作为时间戳导入熊猫数据集 - How to import a CSV with an hour/minute string as a timestamp to panda dataset 编写将整数分钟转换为格式为“X小时Y分钟”的字符串的函数的最佳方法是什么? - What is the best way to write a function that converts an integer of minutes into a string formatted in “X hour(s) Y minute(s)”? 如何仅保留已记录时间序列的小时:分钟:第二部分? (如何将 %Y-%m-%d %H:%M:%S 转换为仅 %H:%M:%S ?) - How to keep only the Hour:Minute:Second part of recorded time series? (How to convert %Y-%m-%d %H:%M:%S to only %H:%M:%S ?) 如何将一天中的小时数和小时数转换为日期时间? - How can I convert hour of day and minute of hour to a datetime? 如何从我的数据集中拆分“小时”和“分钟” - How to split 'Hour' and 'Minute' from my dataset 如何提取字节数组字符串的小时、分钟和秒部分? - How to extract the hour, minute and second part of a bytearray string? 如何将csv转换为线路协议 - How to convert csv's to line protocol 如何使用 Glue 作业将 JSON 从 s3 转换为 CSV 文件并将其保存在同一个 s3 存储桶中 - How to convert JSON to CSV file from s3 and save it in same s3 bucket using Glue job 无法将值转换为轴单位:“小时” - Failed to convert value(s) to axis units: 'hour' 如何从Python中的日期时间戳提取小时:分钟 - How to extract hour:minute from a datetime stamp in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM