我有一个数据帧和一些列。我想对“间隙”列求和,其中时间在某些时隙中。你知道吗
region. date. time. gap
0 1 2016-01-01 00:00:08 1
1 1 2016-01-01 00:00:48 0
2 1 2016-01-01 00:02:50 1
3 1 2016-01-01 00:00:52 0
4 1 2016-01-01 00:10:01 0
5 1 2016-01-01 00:10:03 1
6 1 2016-01-01 00:10:05 0
7 1 2016-01-01 00:10:08 0
我想和gap列相加。我在dict里有这样的时间段。你知道吗
'slot1': '00:00:00', 'slot2': '00:10:00', 'slot3': '00:20:00'
现在求和之后,上面的数据帧应该是这样的。你知道吗
region. date. time. gap
0 1 2016-01-01 00:10:00/slot1 2
1 1 2016-01-01 00:20:00/slot2 1
我有许多地区和144个时段从00:00:00到23:59:49。 我试过这个。你知道吗
regres=reg.groupby(['start_region_hash','Date','Time'])['Time'].apply(lambda x: (x >= hoursdict['slot1']) & (x <= hoursdict['slot2'])).sum()
但它不起作用。你知道吗
我们将非常感谢您的帮助。谢谢
解决这个问题的方法是首先将
time
列转换为所需的值,然后对time
列执行groupby sum
。你知道吗下面的代码显示了我使用的方法。我使用
np.select
来包含任意多个条件和条件选项。在我把time
转换成我想要的值之后,我做了一个简单的groupby sum
不必为格式化时间或转换字符串等操心。简单地让数据帧直观地处理它。你知道吗如果你想保持原来的时间,你可以做
df['timeNew'] = answerlist
,然后从那里过滤。你知道吗为了避免Datetime比较的复杂性(除非这是你的全部观点,在这种情况下忽略我的答案),并通过时隙窗口显示这组问题的本质,我在这里假设时间是整数。你知道吗
输出
想法是将列} 的
time
通过10Min
转换成带有^{datetimes
,然后转换成字符串HH:MM:SS
:按字典聚合} 值,并使用值替换键:
sum
和最后的^{如果要显示下一个
10Min
插槽:编辑:
对floor和convert to string的改进是通过^{} 或
searchsorted
使用bining:相关问题 更多 >
编程相关推荐