1
我有一個帶有3個參數「Date」,「Time」和「Value」的表格。我想在每個10分鐘後面添加一個新的列,該列中的值對應於「時間」。在Pandas中打印相應的列值
我雖然用秒輕鬆創建新列。
現在我想要一個新的列假設「x」將有第一個vlaue和只有10分鐘後對應於「時間」的值。在這種情況下我們能做什麼?
我有一個帶有3個參數「Date」,「Time」和「Value」的表格。我想在每個10分鐘後面添加一個新的列,該列中的值對應於「時間」。在Pandas中打印相應的列值
我雖然用秒輕鬆創建新列。
現在我想要一個新的列假設「x」將有第一個vlaue和只有10分鐘後對應於「時間」的值。在這種情況下我們能做什麼?
IIUC你可以先通過sub
刪除dates
和hours
,加10 Min
,然後轉換爲miliseconds
和劃分1000
:
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