【问题标题】:Printing a corresponding value of a column in Pandas在 Pandas 中打印列的相应值
【发布时间】:2016-12-31 05:58:18
【问题描述】:

我有一个包含 3 个参数“日期”、“时间”和“值”的表格。我想在每 10 分钟之后向该表添加一个新列,该列的值对应于“时间”。

为了方便起见,我想用 Seconds 创建一个新列。

现在我想要一个新列,假设“x”将具有第一个值和仅在 10 分钟后对应于“时间”的值。在这种情况下我们能做什么?

【问题讨论】:

    标签: python datetime pandas dataframe timedelta


    【解决方案1】:

    IIUC你可以先把dateshours去掉sub,加上10 Min,再转换成miliseconds1000

    df = pd.DataFrame({'Time': ['08:01:29:12','08:03:29:12','08:05:29:12'],
                       'Date':['1/2/2016','1/2/2016','1/2/2016']})  
    df['Date'] = pd.to_datetime(df.Date)
    df['Time'] = pd.to_datetime(df.Time, format='%H:%M:%S:%f')
    

    df['new'] = df.Time.sub(df.Time.values.astype('<M8[h]') + 
                pd.offsets.Minute(10)).astype('timedelta64[ms]') / 1000
    
    print (df)
    
            Date                    Time     new
    0 2016-01-02 1900-01-01 08:01:29.120  689.12
    1 2016-01-02 1900-01-01 08:03:29.120  809.12
    2 2016-01-02 1900-01-01 08:05:29.120  929.12
    

    或者:

    df['new'] = df.Time.sub(df.Time.values.astype('<M8[h]') + 
                pd.to_timedelta('00:10:00')).astype('timedelta64[ms]') / 1000
    
    print (df)
            Date                    Time     new
    0 2016-01-02 1900-01-01 08:01:29.120  689.12
    1 2016-01-02 1900-01-01 08:03:29.120  809.12
    2 2016-01-02 1900-01-01 08:05:29.120  929.12
    

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 2016-09-18
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2016-12-10
      相关资源
      最近更新 更多