gpt4 book ai didi

python - 如何在 python 中绘制程序的持续时间

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:00 24 4
gpt4 key购买 nike

我试图绘制一些在夜间运行的程序的持续时间,我将程序持续时间数据导出到 CSV 文件中,以便稍后进行分析。 (像这样)

example

这是我的代码和 CSV 示例:

CSV:

 na,programName,totaal,na,startDate,endDate,Date
?,"to/check.apl",54006,?,2017-02-27T20:04:07.233,2017-02- 27T20:05:01.239,2017-02-27T00:00:00.000
?,"to/ibx.apl",143887,?,2017-02-27T20:07:55.627,2017-02-27T20:10:19.514,2017-02-27T00:00:00.000
?,"to/checker.apl",2039600,?,2017-02-27T20:14:37.662,2017-02-27T20:48:37.262,2017-02-27T00:00:00.000

python 代码:

 import matplotlib
from pandas import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

matplotlib.style.use('ggplot')

data = "miFile.csv"
df = pd.DataFrame.from_csv(data)
df = df.set_index('totaal')

newDf = df[['programName','startDate','endDate']]

到目前为止,我得到了日期时间错误,所以我尝试通过这样做来解决这个问题(也没有运气来绘制):

 newDf['startDate'] = pd.to_datetime(newDf['startDate'])
newDf['endDate'] = pd.to_datetime(newDf['endDate'])

#pd.to_datetime(pd.Series(["2017-02-27T20:04:07.233"]) format= "%d, %m, %y, %H: %M: %S")

newDf.plot('programName','startDate','endDate')

plt.show()

最佳答案

我想你需要read_csv用于创建 df,然后获取列的差异和 convert timedelta分钟 plot :

temp=u"""na,programName,totaal,na,startDate,endDate,Date
?,"to/check.apl",54006,?,2017-02-27T20:04:07.233,2017-02-27T20:05:01.239,2017-02-27T00:00:00.000
?,"to/ibx.apl",143887,?,2017-02-27T20:07:55.627,2017-02-27T20:10:19.514,2017-02-27T00:00:00.000
?,"to/checker.apl",2039600,?,2017-02-27T20:14:37.662,2017-02-27T20:48:37.262,2017-02-27T00:00:00.000"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), index_col=[2], parse_dates=[4,5,6])

print (df.dtypes)
na object
programName object
na.1 object
startDate datetime64[ns]
endDate datetime64[ns]
Date datetime64[ns]
dtype: object
df['duration'] = (df['endDate'] - df['startDate']).astype('timedelta64[m]')
newDf = df[['programName','duration']]
print (newDf)
programName duration
totaal
54006 to/check.apl 0.0
143887 to/ibx.apl 2.0
2039600 to/checker.apl 33.0

newDf.plot()

plt.show()

关于python - 如何在 python 中绘制程序的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668549/

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