我有一个熊猫数据框,如下所示
Key Name Val1 Val2 Timestamp
101 A 10 1 01-10-2019 00:20:21
102 A 12 2 01-10-2019 00:20:21
103 B 10 1 01-10-2019 00:20:26
104 C 20 2 01-10-2019 14:40:45
105 B 21 3 02-10-2019 09:04:06
106 D 24 3 02-10-2019 09:04:12
107 A 24 3 02-10-2019 09:04:14
108 E 32 2 02-10-2019 09:04:20
109 A 10 1 02-10-2019 09:04:22
110 B 10 1 02-10-2019 10:40:49
从最早的时间戳开始,即“01-10-2019 00:20:21”,我需要创建每个10秒的时间槽,并为时间槽中具有时间戳的所有行分配相同的组号。 输出应如下所示
Key Name Val1 Val2 Timestamp Group
101 A 10 1 01-10-2019 00:20:21 1
102 A 12 2 01-10-2019 00:20:21 1
103 B 10 1 01-10-2019 00:20:26 1
104 C 20 2 01-10-2019 14:40:45 2
105 B 21 3 02-10-2019 09:04:06 3
106 D 24 3 02-10-2019 09:04:12 4
107 A 24 3 02-10-2019 09:04:14 4
108 E 32 2 02-10-2019 09:04:20 4
109 A 10 1 02-10-2019 09:04:22 5
110 B 10 1 02-10-2019 10:40:49 6
第一时间栏:“01-10-2019 00:20:21”至“01-10-2019 00:20:30”, 下次bin:“01-10-2019 00:20:31”至“01-10-2019 00:20:40”, 下次bin:“01-10-2019 00:20:41”至“01-10-2019 00:20:50”, 下次bin:“01-10-2019 00:20:51”至“01-10-2019 00:21:00”, 下次bin:“01-10-2019 00:21:01”至“01-10-2019 00:21:10” 等等根据这些时间段,为每行分配“组”。 不强制要求有连续的组号(如果没有时间段,可以跳过该组号)
我已经使用for循环生成了这个,但是如果数据分布在几个月内,则需要很多时间。 请让我知道这是否可以作为一个熊猫操作使用一行代码来完成。谢谢
下面是一个没有
loop
的示例。主要方法是将秒数取整到特定范围,并使用ngroup()
我使用一个新的临时列来查找特定的范围
输出:
您也可以尝试使用TimeGrouper or resample
希望这有帮助
相关问题 更多 >
编程相关推荐