Adding missing data to dataframe grouped by date

时间:2017-12-18 07:38:18

标签: python pandas

I have a Pandas dataframe with datetime column named time. I'd like to count the number of rows per hour. The problem is that I'd like the resulting table handle hours for which no rows exist. Example:

$scope.UpdateTime = function() {

  $scope.totalHours = $scope.currentTimeStartHour - 3;

};  

I can get these values using time id lat lon type 0 2017-06-09 19:34:59.945128-07:00 75 36.999866 -122.058180 UPPER CAMPUS 1 2017-06-09 19:53:56.387058-07:00 75 36.979664 -122.058900 OUT OF SERVICE/SORRY 2 2017-06-09 19:28:53.525189-07:00 75 36.988640 -122.066820 UPPER CAMPUS 3 2017-06-09 19:30:31.633478-07:00 75 36.991657 -122.066605 UPPER CAMPUS which produces:

df.groupby(df.time.dt.hour).count()

Which is correct: 0, 1, 2 are the hours of the day. However, I'd like to represent that there are no rows for hours 3, 4, 5. Having each of these column names is unnecessary, since the value is the same for each.

1 个答案:

答案 0 :(得分:1)

You can use reindex:

mix.styles([
'resources/css/custom.css'
], 'public/css')