我有一个数据帧,日期索引类型是Timestamp,时间列是Date Time.Time:
Time Value
Date
2004-05-01 0:15 3.58507
2004-05-02 0:30 3.84625
...
如何将其转换为:
Value
Date
2004-05-01 0:15 3.74618
2004-05-01 0:30 3.58507
2004-05-01 0:45 3.30998
我写了一个代码,它确实有效,但不是很Python:
ind = frame.index.get_level_values(0).tolist()
tms = frame['Time']
new_ind = []
for i in range(0, len(ind)):
tm = tms[i]
val = ind[i] + timedelta(hours=tm.hour, minutes=tm.minute, seconds=tm.second)
new_ind.append(val)
frame.index = new_ind
del frame['Time']
您可以首先转换列} ,然后添加到} 列
Time
^{index
,^{Time
,并在必要时设置索引name
:如果列
Time
是datetime.time
for me works强制转换为string
第一个(如果需要,请添加:00
):相关问题 更多 >
编程相关推荐