我有以下数据框示例,其中包含日期和间隔:
Date Interval
0 2013-08-01 14:00:00
1 2013-08-01 14:15:00
2 2013-08-01 14:30:00
3 2013-08-01 14:45:00
4 2013-08-01 15:00:00
...
我想要的是一个新的列,其中Interval被映射为:
00:00:00 = 1
00:15:00 = 2
00:30:00 = 3
00:45:00 = 4
...
23:45:00 = 96
所以每15分钟就有1个Intervall。 数据框中的行是混合的,所以我无法启动计数器并增加值。我需要使用Interval Column中的时间值来获取新列
中的映射值我试过了:
dates = pandas.to_datetime(df['Interval'])
df['IntervalMapped']= dates.dt.hour * 2 + dates.dt.minute//15 + 1
但那错了
答案 0 :(得分:0)
IIUC,我想你想要这个:
df['timestamp'] = pd.to_datetime(df['Date'] + ' ' + df['Interval'])
df['IntervalMap'] = df['timestamp'].dt.hour.mul(4) + df['timestamp'].dt.minute.floordiv(15) + 1
输出:
Date Interval timestamp IntervalMap
0 2013-08-01 14:00:00 2013-08-01 14:00:00 57
1 2013-08-01 14:15:00 2013-08-01 14:15:00 58
2 2013-08-01 14:30:00 2013-08-01 14:30:00 59
3 2013-08-01 14:45:00 2013-08-01 14:45:00 60
4 2013-08-01 15:00:00 2013-08-01 15:00:00 61
答案 1 :(得分:0)
创建一个包含最小和最大日期+间隔的系列,并将其映射到日期时间列
df['datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Interval'])
rng = pd.date_range(df['datetime'].min(), df['datetime'].max(), freq = '15T')
s = pd.Series(np.arange(1, len(rng)+1, 1), index = rng)
df['IntervalMapped'] = df['datetime'].map(s)
df.drop('datetime', axis = 1, inplace = True)
Date Interval IntervalMapped
0 2013-08-01 14:00:00 1
1 2013-08-01 14:15:00 2
2 2013-08-01 14:30:00 3
3 2013-08-01 14:45:00 4
4 2013-08-01 15:00:00 5
答案 2 :(得分:0)
我认为您可能想将“hms”表单视为文本并编写自己的计数功能?希望这会有所帮助。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def converter(hms):
"""Takes a time in hms returns number of 15 second intervals after 00:00:00.
"""
h,m,s = hms.split(':')
return int(h)*4+int(m)//15
converter('02:15:00') # 9
path = 'YOUR path'
new_df = pd.read_csv(path+'stack.txt')
new_df['Interval Mapped'] = new_df['Interval'].apply(lambda x: converter(x))
new_df