'Create numpy array

[[6, '22:20:22'], [0, '22:21:54'], [1, '22:25:07'], [2, '06:26:25'], [3, '06:17:34'], [4, '10:39:53'], [6, '10:40:09']]

in this array, the first value is an index and the second value is time.

I want to count given time interval how many index are present in this array and plot like this.

[[50 49 28 71 65 50  7]
 [26 40 61 48 22 14 63]
 [95 85 52  7 36 80 70]
 [20 38  4 16 98 56 14]
 [15 88 14 10  4 84 52]
 [75 53 93 67 44 90 91]
 [ 2 60 66 23 28 53 18]
 [59 65 68 36 81 24 89]
 [76 34 85 56 63 73 36]
 [41  6 17 31  6 50 16]
 [30 28 75 32 89 38 29]
 [ 2 83 76 18 64 41 22]]

in this array, each row represent 0 to 6 and each row represent 00:00:00 to 22:00:00 like this

              0  1  2  3   4  5  6

00:00:00    [[50 49 28 71 65 50  7]
02:00:00     [26 40 61 48 22 14 63]
04:00:00     [95 85 52  7 36 80 70]
06:00:00     [20 38  4 16 98 56 14]
08:00:00     [15 88 14 10  4 84 52]
10:00:00     [75 53 93 67 44 90 91]
12:00:00     [ 2 60 66 23 28 53 18]
14:00:00     [59 65 68 36 81 24 89]
16:00:00     [76 34 85 56 63 73 36]
18:00:00     [41  6 17 31  6 50 16]
20:00:00     [30 28 75 32 89 38 29]
22:00:00     [ 2 83 76 18 64 41 22]]

each value inside the array represents the count of each index in that time period. How can I achieved this



Solution 1:[1]

Your array (labeled as x) can be converted into a dataframe directly

df = pd.DataFrame(x)

Then you may use pd.to_timedelta to convert time into timedelta which allows you to use dt.round to round the timedelta to "every-two-hours"

df[1] = pd.to_datetime(df[1]).dt.round('2h').dt.strftime('%H:%M:%S')

Finally this will give you the table.

df.value_counts().unstack().transpose()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raymond Kwok