简体   繁体   English

如何将小时分钟和秒数放在数据框中而不是年,月和日?

[英]How to just put Hours Minutes and seconds in data frame instead of Year , months and days?

I want to plot a line graph of ECG in mV and time in HH:MM:SS:MMM . 我想绘制心电图的线图,单位为mV ,时间为HH:MM:SS:MMM its a 10 second ECG strip. 它是一个10秒的心电图带。

  1. image of ECG CSV file with two ECG values and time ECG CSV文件的图象有两个ECG值和时间的

I Have extract the Time column and now i want to convert the time column in dataframe of python and then plot it on graph 我已经提取了Time列,现在我想转换python数据帧中的时间列,然后在图上绘制它

but when I apply to_datetime() function it give me the following error 但是当我应用to_datetime()函数时,它会给我以下错误

to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing 要组装映射,至少需要指定[年,月,日]:[日,月,年]缺失

  1. Screenshot of error i get 我得到的错误的屏幕截图

please Help me to resolve this error , I only want to put %H:%M:%S.%f because i don not have the year , months and days. 请帮我解决这个错误,我只想把%H:%M:%S.%f,因为我没有年,月和日。

See to_timedelta to_timedelta

Date : 日期

df1 = {'Time':['11:20:15.333','10:00:00.444'],'P1':['102','102'],'P2':['240','247']}
df1 = pd.DataFrame(data=df1)
df1

Code : 代码

df1['Time'] = pd.to_timedelta(df1['Time'])
df1

Result : 结果

    Time            P1  P2
0   11:20:15.333000 102 240
1   10:00:00.444000 102 247

Reference : https://stackoverflow.com/a/46801500/1855988 参考: https//stackoverflow.com/a/46801500/1855988

You need to specify the format you want to convert the time to. 您需要指定要将时间转换为的format You can find out more information here about what each symbol means. 您可以在此处找到有关每个符号含义的更多信息。

# before it is object
df['column_name'] = pd.to_datetime(df['column_name'], format="%H:%M:%S,%f")
df = df.astype('datetime64')
df['column_name'] =  pd.to_datetime(df['column_name'], format='%H:%M:%S', errors='coerce').dt.time
# coming back to object
print(df.head())
# print(df.info())

As commented you can add a date to those times. 如评论所述,您可以为这些时间添加日期。 The date can be arbitrary. 日期可以是任意的。 Then you can convert to datetime and use them to plot your graph. 然后,您可以转换为datetime并使用它们来绘制图形。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

data = {'Time':['11:20:15.333','12:00:00.444', '13:46:00.100'],
       'A':[1,3,2],'B':[5,5,4]}
df = pd.DataFrame(data=data)
df["Date"] = "2019-09-09"
df['Datetime'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'])

df = df[["Datetime", "A", "B"]].set_index("Datetime")

ax = df.plot(x_compat=True)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S.%f"))

plt.show()

在此输入图像描述

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

相关问题 如何在 Python 中将天转换为小时、分钟和秒 - How transform days to hours, minutes and seconds in Python 如何将以天、小时、分钟和秒为单位的时间转换为仅秒? - How to convert time in days, hours, minutes, and seconds to only seconds? Pandas:从可用数据中导出天、小时、分钟、秒时间戳 - Pandas: Derive a days, hours, minutes, seconds timestamp from available data 将秒转换为天,小时,分钟和秒 - Converting Seconds into days, hours, minutes, and seconds 在 Python 中将秒转换为天、小时、分钟和秒 - Converting seconds into days, hours, minutes & seconds in Python Python计算时差,在1中给出“年、月、日、时、分和秒” - Python calculating time difference, to give ‘years, months, days, hours, minutes and seconds’ in 1 如何在Python / Django中将此持续时间转换为天/小时/分钟/秒? - How to convert this duration into days/hours/minutes/seconds in Python/Django? 如何将“0 days 00:09:06.633000000”对象转换为分钟/小时/秒? - How to convert "0 days 00:09:06.633000000" object to minutes/hours/seconds? Python | 将秒变成天、月、小时的问题 - Python | Issue with turning seconds into days, months, hours 我如何只获得小时、分钟和秒,但分开? - How do I get just the hours, minutes, and seconds, but separately?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM