【问题标题】:converting the column of a dataframe into a timestamp将数据框的列转换为时间戳
【发布时间】:2018-12-03 11:55:24
【问题描述】:

我正在将一个数据框读入 pandas。它是来自 ECG 的时间序列数据。我有两列:第一列是读取时间,第二列是心电图的值。我正在尝试将第一列转换为时间戳并将其用作索引。我的代码如下:

import pandas as pd
from datetime import datetime
from datetime import time
import matplotlib.pyplot as plt
path='/home/user_1/Documents/heart_data.csv'
df=pd.read_csv(path)
df.columns=['Elapsed Time','i']
df['Elapsed Time']=pd.to_datetime(df['Elapsed Time'], format="'%H:%M.%S%f'") 
df.set_index('Elapsed Time', inplace=True)
print(df.head())

我的问题是,这给了我以“1900-01-01 00:00:00.000”形式输出的列,但我不希望只是时间“1900-01-01”。 我怎样才能摆脱出现在我的专栏中的日期?

【问题讨论】:

    标签: python-datetime


    【解决方案1】:
    #Let's create a dataframe.
    df=pd.DataFrame({'date':['1920-01-01 00:00:00.000','1900-01-01 22:00:00.000']},index=[1,2])
    df['date']=pd.to_datetime(df['date'],format='%Y-%m-%d %H:%M:%S.%f')
    df
                     date
    1 1920-01-01 00:00:00
    2 1900-01-01 22:00:00
    
    df=df['date'].dt.time #This will only give you Time part
    
    df
    1    00:00:00
    2    22:00:00
    Name: date, dtype: object
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 2020-05-23
      • 2022-11-30
      • 2022-10-24
      • 2018-11-30
      • 1970-01-01
      • 2016-02-13
      • 2012-08-19
      相关资源
      最近更新 更多