将时间戳转换为广告位。蟒蛇。大熊猫

时间:2019-06-03 10:07:33

标签: python pandas

将上述时间戳转换为时隙。

timestamp      Weather  Location
2014-10-26 00:00    35  1
2014-10-26 06:00    36  1
2014-10-26 12:00    34  1
2014-10-26 18:00    34  1
2014-10-27 00:00    35  1
2014-10-27 06:00    36  1
2014-10-27 12:00    36  1
2014-10-27 18:00    32  1
2014-10-28 00:00    35  1
2014-10-28 06:00    33  1
2014-10-28 12:00    35  1
2014-10-28 18:00    33  1
2014-10-26 00:00    45  2
2014-10-26 06:00    46  2
2014-10-26 12:00    41  2
2014-10-26 18:00    39  2
2014-10-27 00:00    46  2
2014-10-27 06:00    44  2
2014-10-27 12:00    45  2
2014-10-27 18:00    42  2
2014-10-28 00:00    41  2
2014-10-28 06:00    40  2
2014-10-28 12:00    42  2
2014-10-28 18:00    41  2

每个6小时时间戳是一个时隙。 我期望的输出是:

timestamp      Weather  Location    Slot 
2014-10-26 00:00    35  1             1
2014-10-26 06:00    36  1             2
2014-10-26 12:00    34  1             3
2014-10-26 18:00    34  1             4
2014-10-27 00:00    35  1             1
2014-10-27 06:00    36  1             2
2014-10-27 12:00    36  1             3
2014-10-27 18:00    32  1             4
2014-10-28 00:00    35  1             1
2014-10-28 06:00    33  1             2
2014-10-28 12:00    35  1             3
2014-10-28 18:00    33  1             4
2014-10-26 00:00    45  2             1
2014-10-26 06:00    46  2             2
2014-10-26 12:00    41  2             3
2014-10-26 18:00    39  2             4
2014-10-27 00:00    46  2             1
2014-10-27 06:00    44  2             2
2014-10-27 12:00    45  2             3
2014-10-27 18:00    42  2             4
2014-10-28 00:00    41  2             1
2014-10-28 06:00    40  2             2
2014-10-28 12:00    42  2             3
2014-10-28 18:00    41  2             4

插槽必须划分为6小时。

1 个答案:

答案 0 :(得分:2)

您可以将6的小时数floor division加上df.timestamp = pd.to_datetime(df.timestamp) df['slot'] = df.groupby(df.timestamp.dt.hour//6).ngroup()+1 timestamp Weather Location slot 0 2014-10-26 00:00:00 35 1 1 1 2014-10-26 06:00:00 36 1 2 2 2014-10-26 12:00:00 34 1 3 3 2014-10-26 18:00:00 34 1 4 4 2014-10-27 00:00:00 35 1 1 5 2014-10-27 06:00:00 36 1 2 6 2014-10-27 12:00:00 36 1 3 7 2014-10-27 18:00:00 32 1 4 8 2014-10-28 00:00:00 35 1 1 9 2014-10-28 06:00:00 33 1 2 10 2014-10-28 12:00:00 35 1 3 11 2014-10-28 18:00:00 33 1 4 12 2014-10-26 00:00:00 45 2 1 13 2014-10-26 06:00:00 46 2 2 14 2014-10-26 12:00:00 41 2 3 ... 并将其用作自定义的石斑鱼,然后使用GroupBy.ngroup获取每个组的数字:

<html>
<head>
<link rel="stylesheet" href="../fullcalendar-4.1.0/packages/core/main.css"></script>
<link rel="stylesheet" href="../fullcalendar-4.1.0/packages/daygrid/main.css"></script>
<link rel="stylesheet" href="../fullcalendar-4.1.0/packages/list/main.css"></script>
<link rel="stylesheet" href="../fullcalendar-4.1.0/packages/timegrid/main.css"></script>
<script type="text/javascript" src="../fullcalendar-4.1.0/packages/core/main.js"></script>
    <script type="text/javascript" src="../fullcalendar-4.1.0/packages/daygrid/main.js"></script>
    <script type="text/javascript" src="../fullcalendar-4.1.0/packages/timegrid/main.js"></script>
    <script type="text/javascript" src="../fullcalendar-4.1.0/packages/list/main.js"></script>
</head>
<script>
$scope.calendarEl = "";
 $scope.calendar = "";
$scope.event = {events: [{
            title:'test1',
            start: '2019-05-05 21:00',
            end: '2019-05-06 00:00'
          },
          {
            title:'test2',
            start: '2019-05-05 12:00'
        }]};
    $scope.calendarEl = document.getElementById('calendar');

        $scope.calendar = new FullCalendar.Calendar($scope.calendarEl, {
            events: $scope.event.events,
            plugins: [ 'dayGrid','timeGrid','list' ]
        });
         $scope.calendar.render();
    });
</script>
<body ng-app="myApp" ng-controller="myController">
   <div id="calendar" ng-model="eventSources"></div>    
</body>
</html>